vendor/ibexa/admin-ui/src/lib/View/Filter/ContentTranslateViewFilter.php line 85

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\AdminUi\View\Filter;
  8. use Ibexa\AdminUi\Form\Data\ContentTranslationData;
  9. use Ibexa\AdminUi\Form\Data\FormMapper\ContentTranslationMapper;
  10. use Ibexa\ContentForms\Form\Type\Content\ContentEditType;
  11. use Ibexa\Contracts\Core\Repository\ContentService;
  12. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  13. use Ibexa\Contracts\Core\Repository\LanguageService;
  14. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  15. use Ibexa\Contracts\Core\Repository\Values\Content\Language;
  16. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  17. use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
  18. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  19. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\Form\FormFactoryInterface;
  22. use Symfony\Component\Form\FormInterface;
  23. /**
  24.  * Handles content translation form.
  25.  */
  26. class ContentTranslateViewFilter implements EventSubscriberInterface
  27. {
  28.     /** @var \Ibexa\Contracts\Core\Repository\ContentService */
  29.     private $contentService;
  30.     /** @var \Ibexa\Contracts\Core\Repository\LanguageService */
  31.     private $languageService;
  32.     /** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
  33.     private $contentTypeService;
  34.     /** @var \Symfony\Component\Form\FormFactoryInterface */
  35.     private $formFactory;
  36.     /** @var \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
  37.     private $languagePreferenceProvider;
  38.     /**
  39.      * @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
  40.      * @param \Ibexa\Contracts\Core\Repository\LanguageService $languageService
  41.      * @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
  42.      * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
  43.      * @param \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  44.      */
  45.     public function __construct(
  46.         ContentService $contentService,
  47.         LanguageService $languageService,
  48.         ContentTypeService $contentTypeService,
  49.         FormFactoryInterface $formFactory,
  50.         UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  51.     ) {
  52.         $this->contentService $contentService;
  53.         $this->languageService $languageService;
  54.         $this->contentTypeService $contentTypeService;
  55.         $this->formFactory $formFactory;
  56.         $this->languagePreferenceProvider $languagePreferenceProvider;
  57.     }
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleContentTranslateForm'];
  61.     }
  62.     /**
  63.      * @param \Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event
  64.      *
  65.      * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  66.      * @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  67.      * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
  68.      * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  69.      * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
  70.      * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  71.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  72.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  73.      */
  74.     public function handleContentTranslateForm(FilterViewBuilderParametersEvent $event): void
  75.     {
  76.         $controllerAction $event->getParameters()->get('_controller');
  77.         if (
  78.             'Ibexa\Bundle\AdminUi\Controller\ContentEditController::translateAction' !== $controllerAction
  79.         ) {
  80.             return;
  81.         }
  82.         $request $event->getRequest();
  83.         $languageCode $request->attributes->get('toLanguageCode');
  84.         $baseLanguageCode $request->attributes->get('fromLanguageCode');
  85.         $content $this->contentService->loadContent(
  86.             (int)$request->attributes->get('contentId'),
  87.             null !== $baseLanguageCode ? [$baseLanguageCode] : null
  88.         );
  89.         $contentType $this->contentTypeService->loadContentType(
  90.             $content->getContentType()->id,
  91.             $this->languagePreferenceProvider->getPreferredLanguages()
  92.         );
  93.         $toLanguage $this->languageService->loadLanguage($languageCode);
  94.         $fromLanguage $baseLanguageCode $this->languageService->loadLanguage($baseLanguageCode) : null;
  95.         $contentTranslateData $this->resolveContentTranslationData(
  96.             $content,
  97.             $toLanguage,
  98.             $fromLanguage,
  99.             $contentType
  100.         );
  101.         $form $this->resolveContentTranslateForm(
  102.             $contentTranslateData,
  103.             $toLanguage,
  104.             $content
  105.         );
  106.         $event->getParameters()->add(['form' => $form->handleRequest($request)]);
  107.     }
  108.     /**
  109.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  110.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Language $toLanguage
  111.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Language|null $fromLanguage
  112.      * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
  113.      *
  114.      * @return \Ibexa\AdminUi\Form\Data\ContentTranslationData
  115.      *
  116.      * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  117.      * @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  118.      * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
  119.      * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  120.      * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  121.      * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
  122.      */
  123.     private function resolveContentTranslationData(
  124.         Content $content,
  125.         Language $toLanguage,
  126.         ?Language $fromLanguage,
  127.         ContentType $contentType
  128.     ): ContentTranslationData {
  129.         $contentTranslationMapper = new ContentTranslationMapper();
  130.         return $contentTranslationMapper->mapToFormData(
  131.             $content,
  132.             [
  133.                 'language' => $toLanguage,
  134.                 'baseLanguage' => $fromLanguage,
  135.                 'contentType' => $contentType,
  136.             ]
  137.         );
  138.     }
  139.     /**
  140.      * @param \Ibexa\AdminUi\Form\Data\ContentTranslationData $contentUpdate
  141.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Language $toLanguage
  142.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  143.      *
  144.      * @return \Symfony\Component\Form\FormInterface
  145.      */
  146.     private function resolveContentTranslateForm(
  147.         ContentTranslationData $contentUpdate,
  148.         Language $toLanguage,
  149.         Content $content
  150.     ): FormInterface {
  151.         return $this->formFactory->create(
  152.             ContentEditType::class,
  153.             $contentUpdate,
  154.             [
  155.                 'languageCode' => $toLanguage->languageCode,
  156.                 'mainLanguageCode' => $content->contentInfo->mainLanguageCode,
  157.                 'content' => $content,
  158.                 'contentUpdateStruct' => $contentUpdate,
  159.                 'drafts_enabled' => true,
  160.             ]
  161.         );
  162.     }
  163. }
  164. class_alias(ContentTranslateViewFilter::class, 'EzSystems\EzPlatformAdminUi\View\Filter\ContentTranslateViewFilter');