vendor/ibexa/admin-ui/src/lib/EventListener/SetViewParametersListener.php line 156

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\EventListener;
  8. use Ibexa\AdminUi\View\ContentTranslateView;
  9. use Ibexa\ContentForms\Content\View\ContentCreateView;
  10. use Ibexa\ContentForms\Content\View\ContentEditView;
  11. use Ibexa\ContentForms\User\View\UserUpdateView;
  12. use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
  13. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  14. use Ibexa\Contracts\Core\Repository\LocationService;
  15. use Ibexa\Contracts\Core\Repository\Repository;
  16. use Ibexa\Contracts\Core\Repository\UserService;
  17. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  18. use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
  19. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  20. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  21. use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
  22. use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
  23. use Ibexa\Core\MVC\Symfony\MVCEvents;
  24. use Ibexa\Core\MVC\Symfony\View\View;
  25. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\Form\FormInterface;
  28. /**
  29.  * @todo It should use ViewEvents::FILTER_VIEW_PARAMETERS event instead.
  30.  */
  31. class SetViewParametersListener implements EventSubscriberInterface
  32. {
  33.     protected LocationService $locationService;
  34.     protected UserService $userService;
  35.     private Repository $repository;
  36.     private GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider;
  37.     private ConfigResolverInterface $configResolver;
  38.     public function __construct(
  39.         LocationService $locationService,
  40.         UserService $userService,
  41.         Repository $repository,
  42.         ConfigResolverInterface $configResolver,
  43.         GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider
  44.     ) {
  45.         $this->locationService $locationService;
  46.         $this->userService $userService;
  47.         $this->repository $repository;
  48.         $this->configResolver $configResolver;
  49.         $this->groupedContentFormFieldsProvider $groupedContentFormFieldsProvider;
  50.     }
  51.     /**
  52.      * Returns an array of event names this subscriber wants to listen to.
  53.      *
  54.      * @return array The event names to listen to
  55.      */
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             MVCEvents::PRE_CONTENT_VIEW => [
  60.                 ['setContentEditViewTemplateParameters'10],
  61.                 ['setUserUpdateViewTemplateParameters'5],
  62.                 ['setContentTranslateViewTemplateParameters'10],
  63.                 ['setContentCreateViewTemplateParameters'10],
  64.                 ['setContentFieldsParameters'20],
  65.             ],
  66.         ];
  67.     }
  68.     /**
  69.      * @param \Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent $event
  70.      *
  71.      * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException
  72.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  73.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  74.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  75.      */
  76.     public function setContentEditViewTemplateParameters(PreContentViewEvent $event): void
  77.     {
  78.         $contentView $event->getContentView();
  79.         if (!$contentView instanceof ContentEditView) {
  80.             return;
  81.         }
  82.         /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
  83.         $content $contentView->getParameter('content');
  84.         $location $contentView->hasParameter('location') ? $contentView->getParameter('location') : null;
  85.         $isPublished null !== $content->contentInfo->mainLocationId && $content->contentInfo->published;
  86.         $contentView->addParameters([
  87.             'parent_location' => $this->resolveParentLocation($content$location$isPublished),
  88.             'is_published' => $isPublished,
  89.         ]);
  90.         if (!$isPublished) {
  91.             $contentView->addParameters([
  92.                 'parent_locations' => $this->locationService->loadParentLocationsForDraftContent($content->versionInfo),
  93.             ]);
  94.         }
  95.         $contentInfo $content->versionInfo->contentInfo;
  96.         $this->processCreator($contentInfo$contentView);
  97.     }
  98.     /**
  99.      * @param \Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent $event
  100.      *
  101.      * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException
  102.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  103.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  104.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  105.      */
  106.     public function setContentTranslateViewTemplateParameters(PreContentViewEvent $event): void
  107.     {
  108.         $contentView $event->getContentView();
  109.         if (!$contentView instanceof ContentTranslateView) {
  110.             return;
  111.         }
  112.         /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
  113.         $content $contentView->getContent();
  114.         $location $contentView->getLocation();
  115.         $isPublished null !== $content->contentInfo->mainLocationId && $content->contentInfo->published;
  116.         $contentView->addParameters([
  117.             'parent_location' => $this->resolveParentLocation($content$location$isPublished),
  118.             'is_published' => $isPublished,
  119.         ]);
  120.         if (!$isPublished) {
  121.             $contentView->addParameters([
  122.                 'parent_locations' => $this->locationService->loadParentLocationsForDraftContent($content->versionInfo),
  123.             ]);
  124.         }
  125.         $contentInfo $content->versionInfo->contentInfo;
  126.         $this->processCreator($contentInfo$contentView);
  127.     }
  128.     /**
  129.      * @param \Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent $event
  130.      */
  131.     public function setUserUpdateViewTemplateParameters(PreContentViewEvent $event): void
  132.     {
  133.         $contentView $event->getContentView();
  134.         if (!$contentView instanceof UserUpdateView) {
  135.             return;
  136.         }
  137.         /** @var \Ibexa\Contracts\Core\Repository\Values\User\User $user */
  138.         $user $contentView->getParameter('user');
  139.         $contentInfo $user->versionInfo->contentInfo;
  140.         $this->processCreator($contentInfo$contentView);
  141.     }
  142.     /**
  143.      * @param \Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent $event
  144.      */
  145.     public function setContentCreateViewTemplateParameters(PreContentViewEvent $event): void
  146.     {
  147.         $contentView $event->getContentView();
  148.         if (!$contentView instanceof ContentCreateView) {
  149.             return;
  150.         }
  151.         $contentView->addParameters([
  152.             'content_create_struct' => $contentView->getForm()->getData(),
  153.         ]);
  154.     }
  155.     public function setContentFieldsParameters(PreContentViewEvent $event): void
  156.     {
  157.         $view $event->getContentView();
  158.         if (!$view instanceof ContentEditView) {
  159.             return;
  160.         }
  161.         $parameters $view->getParameters();
  162.         $parameters['grouped_fields'] = $this->groupedContentFormFieldsProvider->getGroupedFields(
  163.             $view->getForm()->get('fieldsData')->all()
  164.         );
  165.         $parameters['ignored_content_fields'] = $this->getIgnoredContentFields($view->getForm()->get('fieldsData')->all());
  166.         $view->setParameters($parameters);
  167.     }
  168.     /**
  169.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
  170.      * @param \Ibexa\Core\MVC\Symfony\View\View $contentView
  171.      */
  172.     private function processCreator(ContentInfo $contentInfoView $contentView): void
  173.     {
  174.         try {
  175.             $creator $this->userService->loadUser($contentInfo->ownerId);
  176.         } catch (NotFoundException $exception) {
  177.             $creator null;
  178.         }
  179.         $contentView->addParameters([
  180.             'creator' => $creator,
  181.         ]);
  182.     }
  183.     /**
  184.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  185.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Location|null $location
  186.      * @param bool $isPublished
  187.      *
  188.      * @return \Ibexa\Contracts\Core\Repository\Values\Content\Location
  189.      *
  190.      * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException
  191.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  192.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  193.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  194.      */
  195.     private function resolveParentLocation(Content $content, ?Location $locationbool $isPublished): Location
  196.     {
  197.         if (!$isPublished) {
  198.             $parentLocations $this->repository->sudo(
  199.                 static function (Repository $repository) use ($content) {
  200.                     return $repository->getLocationService()->loadParentLocationsForDraftContent($content->getVersionInfo());
  201.                 }
  202.             );
  203.             return reset($parentLocations);
  204.         }
  205.         if (null === $location) {
  206.             throw new InvalidArgumentException('$location''You must provide a Location for the published Content item');
  207.         }
  208.         return $this->repository->sudo(
  209.             static function (Repository $repository) use ($location) {
  210.                 return $repository->getLocationService()->loadLocation($location->parentLocationId);
  211.             }
  212.         );
  213.     }
  214.     /**
  215.      * @param array<\Symfony\Component\Form\FormInterface> $fieldsData
  216.      *
  217.      * @return array<string>
  218.      */
  219.     private function getIgnoredContentFields(array $fieldsData): array
  220.     {
  221.         $fieldTypeConfig $this->configResolver->getParameter('admin_ui_forms.content_edit.fieldtypes');
  222.         $ignoredFieldTypes array_keys(
  223.             array_filter(
  224.                 $fieldTypeConfig,
  225.                 static fn (array $config): bool => true === $config['meta']
  226.             )
  227.         );
  228.         $ignoredFieldIdentifiers = [];
  229.         foreach ($fieldsData as $fieldIdentifier => $fieldData) {
  230.             if (!in_array($fieldData->getData()->fieldDefinition->fieldTypeIdentifier$ignoredFieldTypestrue)) {
  231.                 continue;
  232.             }
  233.             $ignoredFieldIdentifiers[] = $fieldIdentifier;
  234.         }
  235.         $metaFieldGroups $this->configResolver->getParameter(
  236.             'admin_ui_forms.content_edit.meta_field_groups_list'
  237.         );
  238.         $metaFieldIdentifiers array_keys(
  239.             array_filter(
  240.                 $fieldsData,
  241.                 static fn (FormInterface $field): bool => in_array(
  242.                     $field->getData()->fieldDefinition->fieldGroup,
  243.                     $metaFieldGroups,
  244.                     true
  245.                 )
  246.             )
  247.         );
  248.         return array_merge($ignoredFieldIdentifiers$metaFieldIdentifiers);
  249.     }
  250. }
  251. class_alias(SetViewParametersListener::class, 'EzSystems\EzPlatformAdminUi\EventListener\SetViewParametersListener');