vendor/sylius/resource-bundle/src/Bundle/Form/Type/ResourceTranslationsType.php line 73

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Form\Type;
  12. use Sylius\Component\Resource\Model\TranslatableInterface;
  13. use Sylius\Component\Resource\Model\TranslationInterface;
  14. use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Webmozart\Assert\Assert;
  21. final class ResourceTranslationsType extends AbstractType
  22. {
  23.     /** @var string[] */
  24.     private array $definedLocalesCodes;
  25.     private string $defaultLocaleCode;
  26.     public function __construct(TranslationLocaleProviderInterface $localeProvider)
  27.     {
  28.         $this->definedLocalesCodes $localeProvider->getDefinedLocalesCodes();
  29.         $this->defaultLocaleCode $localeProvider->getDefaultLocaleCode();
  30.     }
  31.     public function buildForm(FormBuilderInterface $builder, array $options): void
  32.     {
  33.         $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
  34.             /** @var TranslationInterface[]|null[] $translations */
  35.             $translations $event->getData();
  36.             $parentForm $event->getForm()->getParent();
  37.             Assert::notNull($parentForm);
  38.             /** @var TranslatableInterface $translatable */
  39.             $translatable $parentForm->getData();
  40.             foreach ($translations as $localeCode => $translation) {
  41.                 if (null === $translation) {
  42.                     unset($translations[$localeCode]);
  43.                     continue;
  44.                 }
  45.                 $translation->setLocale($localeCode);
  46.                 $translation->setTranslatable($translatable);
  47.             }
  48.             $event->setData($translations);
  49.         });
  50.     }
  51.     public function configureOptions(OptionsResolver $resolver): void
  52.     {
  53.         $resolver->setDefaults([
  54.             'entries' => $this->definedLocalesCodes,
  55.             'entry_name' => function (string $localeCode): string {
  56.                 return $localeCode;
  57.             },
  58.             'entry_options' => function (string $localeCode): array {
  59.                 return [
  60.                     'required' => $localeCode === $this->defaultLocaleCode,
  61.                 ];
  62.             },
  63.         ]);
  64.     }
  65.     public function getParent(): string
  66.     {
  67.         return FixedCollectionType::class;
  68.     }
  69.     public function getBlockPrefix(): string
  70.     {
  71.         return 'sylius_translations';
  72.     }
  73. }