vendor/ibexa/admin-ui/src/lib/Form/Processor/TranslationFormProcessor.php line 49

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. namespace Ibexa\AdminUi\Form\Processor;
  7. use Ibexa\AdminUi\Form\Data\ContentTranslationData;
  8. use Ibexa\ContentForms\Data\Content\ContentUpdateData;
  9. use Ibexa\ContentForms\Event\ContentFormEvents;
  10. use Ibexa\ContentForms\Event\FormActionEvent;
  11. use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
  12. use Ibexa\Contracts\Core\Repository\ContentService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. /**
  15. * Listens for and processes RepositoryForm events: publish, remove draft, save draft...
  16. */
  17. class TranslationFormProcessor implements EventSubscriberInterface
  18. {
  19. /** @var \Ibexa\Contracts\Core\Repository\ContentService */
  20. private $contentService;
  21. public function __construct(
  22. ContentService $contentService
  23. ) {
  24. $this->contentService = $contentService;
  25. }
  26. /**
  27. * @return array
  28. */
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. ContentFormEvents::CONTENT_EDIT => ['createContentDraft', 20],
  33. ];
  34. }
  35. /**
  36. * Creates content draft based in data submitted by the user and injects ContentUpdateData to the event.
  37. *
  38. * This step is required to achieve compatibility with other FormProcessors.
  39. *
  40. * @param \Ibexa\ContentForms\Event\FormActionEvent $event
  41. */
  42. public function createContentDraft(FormActionEvent $event): void
  43. {
  44. /** @var \Ibexa\AdminUi\Form\Data\ContentTranslationData $data */
  45. $data = $event->getData();
  46. if (!$data instanceof ContentTranslationData) {
  47. return;
  48. }
  49. $contentDraft = $this->contentService->createContentDraft($data->content->contentInfo);
  50. $fields = array_filter($data->fieldsData, static function (FieldData $fieldData) use ($contentDraft, $data) {
  51. $mainLanguageCode = $contentDraft->getVersionInfo()->getContentInfo()->mainLanguageCode;
  52. return $mainLanguageCode === $data->initialLanguageCode
  53. || ($mainLanguageCode !== $data->initialLanguageCode && $fieldData->fieldDefinition->isTranslatable);
  54. });
  55. $contentUpdateData = new ContentUpdateData([
  56. 'initialLanguageCode' => $data->initialLanguageCode,
  57. 'contentDraft' => $contentDraft,
  58. 'fieldsData' => $fields,
  59. ]);
  60. $event->setData($contentUpdateData);
  61. }
  62. }
  63. class_alias(TranslationFormProcessor::class, 'EzSystems\EzPlatformAdminUi\Form\Processor\TranslationFormProcessor');