app/Plugin/CMBlogPro42/Form/Type/Admin/BlogType.php line 91

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