Skip to content

Commit

Permalink
IBX-7800: Removed Sub-items tab for non-containers without children
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Feb 22, 2024
1 parent ce8ad56 commit a9d7936
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/contracts/Tab/AbstractEventDispatchingTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function renderView(array $parameters): string
);
}

/**
* @param array<string, mixed> $parameters
*/
public function isEnabled(array $parameters): bool
{
return true;
}

abstract public function getTemplate(): string;

/**
Expand Down
10 changes: 9 additions & 1 deletion src/lib/Component/TabsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions src/lib/Tab/LocationView/SubItemsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 */
Expand Down

0 comments on commit a9d7936

Please sign in to comment.