vendor/ibexa/site-context/src/lib/Menu/ConfigureRightMenuSidebarListener.php line 47

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\SiteContext\Menu;
  8. use Ibexa\AdminUi\Menu\ContentRightSidebarBuilder;
  9. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  10. use Ibexa\Contracts\Core\Repository\PermissionResolver;
  11. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  12. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  13. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  14. use Ibexa\Contracts\SiteContext\SiteContextServiceInterface;
  15. use Ibexa\Core\MVC\Symfony\SiteAccess;
  16. use Ibexa\SiteContext\Specification\IsContextAware;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. final class ConfigureRightMenuSidebarListener
  19. {
  20.     private SiteContextServiceInterface $siteAccessService;
  21.     private PermissionResolver $permissionResolver;
  22.     private UrlGeneratorInterface $urlGenerator;
  23.     private ConfigResolverInterface $configResolver;
  24.     public function __construct(
  25.         SiteContextServiceInterface $siteAccessService,
  26.         PermissionResolver $permissionResolver,
  27.         UrlGeneratorInterface $urlGenerator,
  28.         ConfigResolverInterface $configResolver
  29.     ) {
  30.         $this->siteAccessService $siteAccessService;
  31.         $this->permissionResolver $permissionResolver;
  32.         $this->urlGenerator $urlGenerator;
  33.         $this->configResolver $configResolver;
  34.     }
  35.     /**
  36.      * @throws \InvalidArgumentException
  37.      */
  38.     public function onAdminUiMenuConfigure(ConfigureMenuEvent $event): void
  39.     {
  40.         $root $event->getMenu();
  41.         $previewItem $root->getChild(ContentRightSidebarBuilder::ITEM__PREVIEW);
  42.         if (!$previewItem) {
  43.             return;
  44.         }
  45.         $siteAccessContext $this->siteAccessService->getCurrentContext();
  46.         $options $event->getOptions();
  47.         /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location $location */
  48.         $location $options['location'];
  49.         /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
  50.         $content $options['content'];
  51.         if ($location instanceof Location && !$this->isContextAware($location)) {
  52.             $root->removeChild($previewItem->getName());
  53.             return;
  54.         }
  55.         if (!$siteAccessContext) {
  56.             return;
  57.         }
  58.         $siteAccessUri $this->getPreviewUri($location$content$siteAccessContext);
  59.         $previewItem->setUri($siteAccessUri);
  60.     }
  61.     private function getPreviewUri(
  62.         Location $location,
  63.         Content $content,
  64.         SiteAccess $siteAccess
  65.     ): ?string {
  66.         $canPreview $this->permissionResolver->canUser(
  67.             'content',
  68.             'versionread',
  69.             $content,
  70.             [$location]
  71.         );
  72.         if (!$canPreview) {
  73.             return null;
  74.         }
  75.         $languageCode $content->contentInfo->mainLanguageCode;
  76.         return $this->urlGenerator->generate('ibexa.content.preview', [
  77.             'contentId' => $content->contentInfo->getId(),
  78.             'versionNo' => $content->getVersionInfo()->versionNo,
  79.             'languageCode' => $languageCode,
  80.             'locationId' => $location->id,
  81.             'preselectedSiteAccess' => $siteAccess->name,
  82.             'referrer' => 'content_view',
  83.         ]);
  84.     }
  85.     private function isContextAware(Location $location): bool
  86.     {
  87.         return IsContextAware::fromConfiguration($this->configResolver)->isSatisfiedBy($location);
  88.     }
  89. }