app/Plugin/CMBlogPro/Form/Type/Admin/BlogType.php line 105

Open in your IDE?
  1. <?php
  2. namespace Plugin\CMBlogPro\Form\Type\Admin;
  3. use Plugin\CMBlogPro\Entity\Blog;
  4. use Plugin\CMBlogPro\Entity\Category;
  5. use Plugin\CMBlogPro\Repository\BlogRepository;
  6. use Plugin\CMBlogPro\Repository\CategoryRepository;
  7. use Plugin\CMBlogPro\Form\Type\Admin\BlogStatusType;
  8. use Plugin\CMBlogPro\Form\Validator\Hankaku;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  11. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  12. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  13. use Symfony\Component\Form\Extension\Core\Type\DateType;
  14. use Symfony\Component\Form\Extension\Core\Type\FileType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. use Symfony\Component\Validator\Constraints\File;
  20. use Symfony\Component\Validator\Constraints\Length;
  21. use Symfony\Component\Validator\Constraints\NotBlank;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. use Symfony\Component\Validator\Constraints\Regex;
  24. use Plugin\CMBlogPro\Repository\ProductRepository;
  25. use Eccube\Entity\Product;
  26. class BlogType extends AbstractType
  27. {
  28.     /**
  29.      * @var BlogRepository
  30.      */
  31.     protected $blogRepository;
  32.     /**
  33.      * @var CategoryRepository
  34.      */
  35.     protected $categoryRepository;
  36.     /**
  37.      * @var ProductRepository
  38.      */
  39.     protected $productRepository;
  40.     /**
  41.      * BlogType constructor.
  42.      *
  43.      * @param BlogRepository $blogRepository
  44.      * @param CategoryRepository $categoryRepository
  45.      * @param ProductRepository $productRepository
  46.      */
  47.     public function __construct(
  48.         BlogRepository $blogRepository,
  49.         CategoryRepository $categoryRepository,
  50.         ProductRepository $productRepository
  51.     ) {
  52.         $this->blogRepository $blogRepository;
  53.         $this->categoryRepository $categoryRepository;
  54.         $this->productRepository  $productRepository;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function buildForm(FormBuilderInterface $builder, array $options)
  60.     {
  61.         $builder
  62.         ->add('title'TextType::class, [
  63.             'required' => true,
  64.             'constraints' => [
  65.                 new NotBlank(),
  66.                 new Length(['max' => 200]),
  67.             ],
  68.         ])
  69.         ->add('slug'TextType::class, [
  70.             'required' => false,
  71.             'constraints' => [
  72.                 new Length(['max' => 255]),
  73.                 new Assert\Regex(array(
  74.                     'pattern'   => "/[\/\:\!\"\'\#\$\%\&\(\)\=\~\^\.\<\>\|\*\;\{\}\+\?]+/",
  75.                     'match'     => false,
  76.                     "message"   => "記号は利用できません。"
  77.                 ))
  78.             ],
  79.         ])
  80.         ->add('Category'ChoiceType::class, [
  81.             'choice_label' => 'name',
  82.             'multiple' => true,
  83.             'mapped' => false,
  84.             'expanded' => true,
  85.             'choices' => $this->categoryRepository->getList(array()),
  86.             'choice_value' => function (Category $Category null) {
  87.                 return $Category $Category->getId() : null;
  88.             },
  89.         ])
  90.         ->add('Product'ChoiceType::class, [
  91.             'choice_label' => 'name',
  92.             'multiple' => true,
  93.             'mapped' => false,
  94.             'expanded' => false,
  95.             'choices' => $this->productRepository->getList(array()),
  96.             'choice_value' => function (Product $Product null) {
  97.                 return $Product $Product->getId() : null;
  98.             },
  99.         ])
  100.         ->add('tag'TextType::class, [
  101.             'required' => false,
  102.             'attr'      => array(
  103.                 'placeholder' => '例:おすすめ,ビックアップ,注目,広告',
  104.             ),
  105.             'constraints' => [
  106.                 new Length(['max' => 1000]),
  107.             ],
  108.         ])
  109.         ->add('product_image'FileType::class, [
  110.             'multiple' => true,
  111.             'required' => false,
  112.             'mapped' => false,
  113.         ])
  114.         
  115.         // ->add('main_image', TextType::class, [
  116.         //     'required' => false,
  117.         //     'mapped' => false,
  118.         // ])
  119.         // ->add('image', FileType::class, [
  120.         //     'mapped' => false,
  121.         //     'required' => false,
  122.         //     'constraints' => [
  123.         //         new File([
  124.         //             'maxSize' => '1024k',
  125.         //             'mimeTypes' => [
  126.         //                 'image/jpeg',
  127.         //                 'image/gif',
  128.         //                 'image/png',
  129.         //                 'image/tiff'
  130.         //             ],
  131.         //             'mimeTypesMessage' => '有効な画像をアップロードして下さい'
  132.         //         ])
  133.         //     ]
  134.         // ])
  135.         // 画像
  136.         ->add('images'CollectionType::class, [
  137.             'entry_type' => HiddenType::class,
  138.             'prototype' => true,
  139.             'mapped' => false,
  140.             'allow_add' => true,
  141.             'allow_delete' => true,
  142.         ])
  143.         ->add('add_images'CollectionType::class, [
  144.             'entry_type' => HiddenType::class,
  145.             'prototype' => true,
  146.             'mapped' => false,
  147.             'allow_add' => true,
  148.             'allow_delete' => true,
  149.         ])
  150.         ->add('delete_images'CollectionType::class, [
  151.             'entry_type' => HiddenType::class,
  152.             'prototype' => true,
  153.             'mapped' => false,
  154.             'allow_add' => true,
  155.             'allow_delete' => true,
  156.         ])
  157.         ->add('return_link'HiddenType::class, [
  158.             'mapped' => false,
  159.         ])
  160.         ->add('body'TextareaType::class, [
  161.             'attr' => [
  162.                 'rows' => 20,
  163.             ],
  164.             'required' => false,
  165.         ])
  166.         ->add('author'TextType::class, [
  167.             'required' => false,
  168.             'constraints' => [
  169.                 new Length(['max' => 255]),
  170.             ],
  171.         ])
  172.         ->add('description'TextType::class, [
  173.             'required' => false,
  174.             'constraints' => [
  175.                 new Length(['max' => 255]),
  176.             ],
  177.         ])
  178.         ->add('keyword'TextType::class, [
  179.             'required' => false,
  180.             'constraints' => [
  181.                 new Length(['max' => 255]),
  182.             ],
  183.         ])
  184.         ->add('robot'TextType::class, [
  185.             'required' => false,
  186.             'constraints' => [
  187.                 new Length(['max' => 255]),
  188.             ],
  189.         ])
  190.         ->add('metatag'TextareaType::class, [
  191.             'required' => false,
  192.         ])
  193.         ->add('release_date'DateType::class, [
  194.             'required'  => true,
  195.             'widget'    => 'single_text',
  196.             'attr'      => array(
  197.                 'placeholder' => 'yyyy-MM-dd',
  198.             ),
  199.             'constraints' => [
  200.                 new NotBlank(),
  201.             ],
  202.             'format'    => 'yyyy-MM-dd'
  203.         ])
  204.         ->add('status'BlogStatusType::class, [
  205.             'constraints' => [
  206.                 new NotBlank(),
  207.             ],
  208.         ]);
  209.     }
  210.     /**
  211.      * {@inheritdoc}
  212.      */
  213.     public function configureOptions(OptionsResolver $resolver)
  214.     {
  215.         $resolver->setDefaults([
  216.             'data_class' => Blog::class,
  217.         ]);
  218.     }
  219.     /**
  220.      * {@inheritdoc}
  221.      */
  222.     public function getBlockPrefix()
  223.     {
  224.         return 'CMBlogPro_admin_blog';
  225.     }
  226. }