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

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