<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\AdminUi\Tab\Event\Subscriber;
use Ibexa\AdminUi\Tab\Event\TabEvents;
use Ibexa\AdminUi\Tab\Event\TabGroupEvent;
use Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Reorders tabs according to their Order value (Tabs implementing OrderedTabInterface).
* Tabs without order specified are pushed to the end of the group.
*
* @see \Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface
*/
class OrderedTabSubscriber implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
TabEvents::TAB_GROUP_PRE_RENDER => ['onTabGroupPreRender'],
];
}
/**
* @param \Ibexa\AdminUi\Tab\Event\TabGroupEvent $tabGroupEvent
*/
public function onTabGroupPreRender(TabGroupEvent $tabGroupEvent)
{
$tabGroup = $tabGroupEvent->getData();
$tabs = $tabGroup->getTabs();
$tabs = $this->reorderTabs($tabs);
$tabGroup->setTabs($tabs);
$tabGroupEvent->setData($tabGroup);
}
/**
* @param \Ibexa\Contracts\AdminUi\Tab\TabInterface[] $tabs
*
* @return array
*/
private function reorderTabs($tabs): array
{
$orderedTabs = [];
foreach ($tabs as $tab) {
if ($tab instanceof OrderedTabInterface) {
$orderedTabs[$tab->getIdentifier()] = $tab;
unset($tabs[$tab->getIdentifier()]);
}
}
uasort($orderedTabs, [$this, 'sortTabs']);
return array_merge($orderedTabs, $tabs);
}
/**
* @param \Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface $tab1
* @param \Ibexa\Contracts\AdminUi\Tab\OrderedTabInterface $tab2
*
* @return int
*/
private function sortTabs(OrderedTabInterface $tab1, OrderedTabInterface $tab2): int
{
return $tab1->getOrder() <=> $tab2->getOrder();
}
}
class_alias(OrderedTabSubscriber::class, 'EzSystems\EzPlatformAdminUi\Tab\Event\Subscriber\OrderedTabSubscriber');