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

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('members'ChoiceType::class, [
  66.             'choices'  => [
  67.                 '一般公開' => 0,
  68.                 '会員限定' => 1,
  69.             ],
  70.         ])
  71.         ->add('linkurl'TextType::class, [
  72.             'required' => false,
  73.         ])
  74.         ->add('slug'TextType::class, [
  75.             'required' => false,
  76.             'constraints' => [
  77.                 new Length(['max' => 255]),
  78.                 new Assert\Regex(array(
  79.                     'pattern'   => "/[\/\:\!\"\'\#\$\%\&\(\)\=\~\^\.\<\>\|\*\;\{\}\+\?]+/",
  80.                     'match'     => false,
  81.                     "message"   => "記号は利用できません。"
  82.                 ))
  83.             ],
  84.         ])
  85.         ->add('Category'ChoiceType::class, [
  86.             'choice_label' => 'name',
  87.             'multiple' => true,
  88.             'mapped' => false,
  89.             'expanded' => true,
  90.             'choices' => $this->categoryRepository->getList(array()),
  91.             'choice_value' => function (Category $Category null) {
  92.                 return $Category $Category->getId() : null;
  93.             },
  94.         ])
  95.         //  ymk changes
  96.         ->add('BlogProduct'CollectionType::class, [
  97.             'entry_type' => BlogProductType::class,
  98.             'allow_add' => true,
  99.             'allow_delete' => true,
  100.             'prototype' => true,
  101.         ])
  102.         // end changes
  103.         ->add('tag'TextType::class, [
  104.             'required' => false,
  105.             'attr'      => array(
  106.                 'placeholder' => '例:おすすめ,ビックアップ,注目,広告',
  107.             ),
  108.             'constraints' => [
  109.                 new Length(['max' => 1000]),
  110.             ],
  111.         ])
  112.         ->add('product_image'FileType::class, [
  113.             'multiple' => true,
  114.             'required' => false,
  115.             'mapped' => false,
  116.         ])
  117.         ->add('fig_caption'TextType::class, [
  118.             'required' => false,
  119.             'constraints' => [
  120.                 new Length(['max' => 255]),
  121.             ],
  122.         ])
  123.         // 画像
  124.         ->add('images'CollectionType::class, [
  125.             'entry_type' => HiddenType::class,
  126.             'prototype' => true,
  127.             'mapped' => false,
  128.             'allow_add' => true,
  129.             'allow_delete' => true,
  130.         ])
  131.         ->add('add_images'CollectionType::class, [
  132.             'entry_type' => HiddenType::class,
  133.             'prototype' => true,
  134.             'mapped' => false,
  135.             'allow_add' => true,
  136.             'allow_delete' => true,
  137.         ])
  138.         ->add('delete_images'CollectionType::class, [
  139.             'entry_type' => HiddenType::class,
  140.             'prototype' => true,
  141.             'mapped' => false,
  142.             'allow_add' => true,
  143.             'allow_delete' => true,
  144.         ])
  145.         ->add('return_link'HiddenType::class, [
  146.             'mapped' => false,
  147.         ])
  148.         ->add('body'TextareaType::class, [
  149.             'attr' => [
  150.                 'rows' => 20,
  151.             ],
  152.             'required' => false,
  153.         ])
  154.         ->add('author'TextType::class, [
  155.             'required' => false,
  156.             'constraints' => [
  157.                 new Length(['max' => 255]),
  158.             ],
  159.         ])
  160.         ->add('description'TextType::class, [
  161.             'required' => false,
  162.             'constraints' => [
  163.                 new Length(['max' => 255]),
  164.             ],
  165.         ])
  166.         ->add('keyword'TextType::class, [
  167.             'required' => false,
  168.             'constraints' => [
  169.                 new Length(['max' => 255]),
  170.             ],
  171.         ])
  172.         ->add('robot'TextType::class, [
  173.             'required' => false,
  174.             'constraints' => [
  175.                 new Length(['max' => 255]),
  176.             ],
  177.         ])
  178.         ->add('metatag'TextareaType::class, [
  179.             'required' => false,
  180.         ])
  181.         ->add('release_date'DateType::class, [
  182.             'required'  => true,
  183.             'widget'    => 'single_text',
  184.             'attr'      => array(
  185.                 'placeholder' => 'yyyy-MM-dd',
  186.             ),
  187.             'constraints' => [
  188.                 new NotBlank(),
  189.             ],
  190.             'format'    => 'yyyy-MM-dd'
  191.         ])
  192.         ->add('status'BlogStatusType::class, [
  193.             'constraints' => [
  194.                 new NotBlank(),
  195.             ],
  196.         ]);
  197.     }
  198.     /**
  199.      * {@inheritdoc}
  200.      */
  201.     public function configureOptions(OptionsResolver $resolver)
  202.     {
  203.         $resolver->setDefaults([
  204.             'data_class' => Blog::class,
  205.         ]);
  206.     }
  207.     /**
  208.      * {@inheritdoc}
  209.      */
  210.     public function getBlockPrefix()
  211.     {
  212.         return 'CMBlogPro_admin_blog';
  213.     }
  214. }