src/Cms/TenantBundle/Form/Type/TenantTypeType.php line 16

Open in your IDE?
  1. <?php
  2. namespace Cms\TenantBundle\Form\Type;
  3. use Cms\CoreBundle\Form\Type\ChosenChoiceType;
  4. use Cms\TenantBundle\Entity\TenantTypeEmbeddable;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\CallbackTransformer;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. /**
  10.  * Class ProductsType
  11.  * @package Cms\TenantBundle\Form\Type
  12.  */
  13. class TenantTypeType extends AbstractType
  14. {
  15.     /**
  16.      * {@inheritdoc}
  17.      */
  18.     public function buildForm(FormBuilderInterface $builder, array $options)
  19.     {
  20.         $builder->addModelTransformer(new CallbackTransformer(
  21.             function (TenantTypeEmbeddable $type) {
  22.                 $value trim(sprintf(
  23.                     '%s.%s',
  24.                     $type->getPrimary(),
  25.                     $type->getSecondary()
  26.                 ));
  27.                 if (empty($value)) {
  28.                     return null;
  29.                 }
  30.                 return $value;
  31.             },
  32.             function (?string $type null) use ($builder) {
  33.                 if (empty($type)) {
  34.                     return new TenantTypeEmbeddable();
  35.                 }
  36.                 $types explode('.'$type);
  37.                 if (count($types) === 1) {
  38.                     $types[1] = null;
  39.                 }
  40.                 return (new TenantTypeEmbeddable())
  41.                     ->setPrimary($types[0])
  42.                     ->setSecondary($types[1]);
  43.             }
  44.         ));
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function configureOptions(OptionsResolver $resolver)
  50.     {
  51.         $resolver->setDefaults([
  52.             'empty_data' => null,
  53.             'required' => false,
  54.             'choices' => (function () {
  55.                 $choices = [
  56.                     '...' => null,
  57.                 ];
  58.                 foreach (TenantTypeEmbeddable::SECONDARY as $primary => $secondaries) {
  59.                     foreach ($secondaries as $secondary) {
  60.                         $key sprintf(
  61.                             'campussuite.cms.tenant.types.combined.%s.%s',
  62.                             $primary,
  63.                             $secondary
  64.                         );
  65.                         $choices[$key] = sprintf(
  66.                             '%s.%s',
  67.                             $primary,
  68.                             $secondary
  69.                         );
  70.                     }
  71.                 }
  72.                 return $choices;
  73.             })(),
  74.         ]);
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function getParent(): ?string
  80.     {
  81.         return ChosenChoiceType::class;
  82.     }
  83. }