From ba08aabc771a46d3594639adb9471b4457281fab Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Thu, 22 Feb 2024 10:27:21 +0100 Subject: [PATCH 1/2] IBX-7800: Removed Sub-items tab for non-containers without children --- .../Tab/AbstractEventDispatchingTab.php | 8 +++++ src/lib/Component/TabsComponent.php | 10 ++++++- src/lib/Tab/LocationView/SubItemsTab.php | 30 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/contracts/Tab/AbstractEventDispatchingTab.php b/src/contracts/Tab/AbstractEventDispatchingTab.php index 09f1bdbd07..bc38a22265 100644 --- a/src/contracts/Tab/AbstractEventDispatchingTab.php +++ b/src/contracts/Tab/AbstractEventDispatchingTab.php @@ -49,6 +49,14 @@ public function renderView(array $parameters): string ); } + /** + * @param array $parameters + */ + public function isEnabled(array $parameters): bool + { + return true; + } + abstract public function getTemplate(): string; /** diff --git a/src/lib/Component/TabsComponent.php b/src/lib/Component/TabsComponent.php index e0ea071195..f2b1629713 100644 --- a/src/lib/Component/TabsComponent.php +++ b/src/lib/Component/TabsComponent.php @@ -13,6 +13,7 @@ use Ibexa\AdminUi\Tab\Event\TabGroupEvent; use Ibexa\AdminUi\Tab\TabGroup; use Ibexa\Contracts\AdminUi\Component\Renderable; +use Ibexa\Contracts\AdminUi\Tab\AbstractEventDispatchingTab; use Ibexa\Contracts\AdminUi\Tab\TabInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Twig\Environment; @@ -64,7 +65,14 @@ public function render(array $parameters = []): string foreach ($tabGroupEvent->getData()->getTabs() as $tab) { $tabEvent = $this->dispatchTabPreRenderEvent($tab, $parameters); $parameters = array_merge($parameters, $tabGroupEvent->getParameters(), $tabEvent->getParameters()); - $tabs[] = $this->composeTabParameters($tabEvent->getData(), $parameters); + // BC Safe: @todo move AbstractEventDispatchingTab::isEnabled to TabInterface + $isEnabled = $tab instanceof AbstractEventDispatchingTab + ? $tab->isEnabled($parameters) + : true; + + if ($isEnabled) { + $tabs[] = $this->composeTabParameters($tabEvent->getData(), $parameters); + } } return $this->twig->render( diff --git a/src/lib/Tab/LocationView/SubItemsTab.php b/src/lib/Tab/LocationView/SubItemsTab.php index c6457cf248..a64125bbff 100644 --- a/src/lib/Tab/LocationView/SubItemsTab.php +++ b/src/lib/Tab/LocationView/SubItemsTab.php @@ -10,10 +10,27 @@ use Ibexa\Contracts\AdminUi\Tab\AbstractEventDispatchingTab; use Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface; +use Ibexa\Contracts\Core\Repository\LocationService; use JMS\TranslationBundle\Annotation\Desc; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\Translation\TranslatorInterface; +use Twig\Environment; class SubItemsTab extends AbstractEventDispatchingTab implements OrderedTabInterface { + private LocationService $locationService; + + public function __construct( + Environment $twig, + TranslatorInterface $translator, + EventDispatcherInterface $eventDispatcher, + LocationService $locationService + ) { + parent::__construct($twig, $translator, $eventDispatcher); + + $this->locationService = $locationService; + } + public const URI_FRAGMENT = 'ibexa-tab-location-view-sub-items'; public function getIdentifier(): string @@ -37,6 +54,19 @@ public function getTemplate(): string return '@ibexadesign/content/tab/sub_items.html.twig'; } + public function isEnabled(array $parameters): bool + { + /** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType */ + $contentType = $parameters['contentType']; + + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location $location */ + $location = $parameters['location']; + + $hasChildren = $this->locationService->getLocationChildCount($location) > 0; + + return $contentType->isContainer && !$hasChildren; + } + public function getTemplateParameters(array $contextParameters = []): array { /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */ From 84fda4f9c22eb290e06e7746b8640f1653a193db Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Fri, 1 Mar 2024 08:23:13 +0100 Subject: [PATCH 2/2] fix: Code Review --- src/contracts/Tab/AbstractEventDispatchingTab.php | 8 -------- src/lib/Component/TabsComponent.php | 9 --------- src/lib/Tab/LocationView/SubItemsTab.php | 5 +++-- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/src/contracts/Tab/AbstractEventDispatchingTab.php b/src/contracts/Tab/AbstractEventDispatchingTab.php index bc38a22265..09f1bdbd07 100644 --- a/src/contracts/Tab/AbstractEventDispatchingTab.php +++ b/src/contracts/Tab/AbstractEventDispatchingTab.php @@ -49,14 +49,6 @@ public function renderView(array $parameters): string ); } - /** - * @param array $parameters - */ - public function isEnabled(array $parameters): bool - { - return true; - } - abstract public function getTemplate(): string; /** diff --git a/src/lib/Component/TabsComponent.php b/src/lib/Component/TabsComponent.php index f2b1629713..bf0f782144 100644 --- a/src/lib/Component/TabsComponent.php +++ b/src/lib/Component/TabsComponent.php @@ -13,7 +13,6 @@ use Ibexa\AdminUi\Tab\Event\TabGroupEvent; use Ibexa\AdminUi\Tab\TabGroup; use Ibexa\Contracts\AdminUi\Component\Renderable; -use Ibexa\Contracts\AdminUi\Tab\AbstractEventDispatchingTab; use Ibexa\Contracts\AdminUi\Tab\TabInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Twig\Environment; @@ -65,14 +64,6 @@ public function render(array $parameters = []): string foreach ($tabGroupEvent->getData()->getTabs() as $tab) { $tabEvent = $this->dispatchTabPreRenderEvent($tab, $parameters); $parameters = array_merge($parameters, $tabGroupEvent->getParameters(), $tabEvent->getParameters()); - // BC Safe: @todo move AbstractEventDispatchingTab::isEnabled to TabInterface - $isEnabled = $tab instanceof AbstractEventDispatchingTab - ? $tab->isEnabled($parameters) - : true; - - if ($isEnabled) { - $tabs[] = $this->composeTabParameters($tabEvent->getData(), $parameters); - } } return $this->twig->render( diff --git a/src/lib/Tab/LocationView/SubItemsTab.php b/src/lib/Tab/LocationView/SubItemsTab.php index a64125bbff..f73a7989be 100644 --- a/src/lib/Tab/LocationView/SubItemsTab.php +++ b/src/lib/Tab/LocationView/SubItemsTab.php @@ -9,6 +9,7 @@ namespace Ibexa\AdminUi\Tab\LocationView; use Ibexa\Contracts\AdminUi\Tab\AbstractEventDispatchingTab; +use Ibexa\Contracts\AdminUi\Tab\ConditionalTabInterface; use Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface; use Ibexa\Contracts\Core\Repository\LocationService; use JMS\TranslationBundle\Annotation\Desc; @@ -16,7 +17,7 @@ use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Environment; -class SubItemsTab extends AbstractEventDispatchingTab implements OrderedTabInterface +class SubItemsTab extends AbstractEventDispatchingTab implements OrderedTabInterface, ConditionalTabInterface { private LocationService $locationService; @@ -54,7 +55,7 @@ public function getTemplate(): string return '@ibexadesign/content/tab/sub_items.html.twig'; } - public function isEnabled(array $parameters): bool + public function evaluate(array $parameters): bool { /** @var \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType */ $contentType = $parameters['contentType'];