vendor/ibexa/dashboard/src/lib/EventSubscriber/ContentCreateContentTypeChoiceLoaderSubscriber.php line 46

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\Dashboard\EventSubscriber;
  8. use Ibexa\AdminUi\Form\Type\Event\ContentCreateContentTypeChoiceLoaderEvent;
  9. use Ibexa\Contracts\Core\Persistence\Content\Location\Handler;
  10. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  11. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  12. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  13. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  14. use Ibexa\Dashboard\Specification\IsDashboardContentType;
  15. use Ibexa\Dashboard\Specification\IsInDashboardTreeRoot;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. final class ContentCreateContentTypeChoiceLoaderSubscriber implements EventSubscriberInterface
  18. {
  19.     private ConfigResolverInterface $configResolver;
  20.     private ContentTypeService $contentTypeService;
  21.     private Handler $locationHandler;
  22.     public function __construct(
  23.         ConfigResolverInterface $configResolver,
  24.         ContentTypeService $contentTypeService,
  25.         Handler $locationHandler
  26.     ) {
  27.         $this->configResolver $configResolver;
  28.         $this->contentTypeService $contentTypeService;
  29.         $this->locationHandler $locationHandler;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ContentCreateContentTypeChoiceLoaderEvent::RESOLVE_CONTENT_TYPES => 'removeDashboardContentType',
  35.         ];
  36.     }
  37.     public function removeDashboardContentType(ContentCreateContentTypeChoiceLoaderEvent $event): void
  38.     {
  39.         $location $event->getTargetLocation();
  40.         if ($location === null) {
  41.             return;
  42.         }
  43.         try {
  44.             $dashboardContentType $this->contentTypeService->loadContentTypeByIdentifier(
  45.                 $this->configResolver->getParameter(
  46.                     IsDashboardContentType::CONTENT_TYPE_IDENTIFIER_PARAM_NAME
  47.                 )
  48.             );
  49.         } catch (NotFoundException $e) {
  50.             return;
  51.         }
  52.         $dashboardGroups $dashboardContentType->getContentTypeGroups();
  53.         if ($this->isInDashboardTreeRoot($location)) {
  54.             $groups = [];
  55.             $dashboardContainer $this->configResolver->getParameter(
  56.                 'dashboard.container_content_type_identifier'
  57.             );
  58.             $folderContentType $this->contentTypeService->loadContentTypeByIdentifier(
  59.                 $dashboardContainer
  60.             );
  61.             foreach ($dashboardGroups as $dashboardGroup) {
  62.                 $groups[$dashboardGroup->identifier] = [
  63.                     $dashboardContentType,
  64.                     $folderContentType,
  65.                 ];
  66.             }
  67.             $event->setContentTypeGroups($groups);
  68.         } else {
  69.             foreach ($dashboardGroups as $dashboardGroup) {
  70.                 $event->removeContentTypeGroup($dashboardGroup->identifier);
  71.             }
  72.         }
  73.     }
  74.     private function isInDashboardTreeRoot(Location $location): bool
  75.     {
  76.         return (new IsInDashboardTreeRoot(
  77.             $this->configResolver,
  78.             $this->locationHandler
  79.         ))->isSatisfiedBy($location);
  80.     }
  81. }