vendor/ezsystems/ezplatform-admin-ui/src/lib/Tab/Event/Subscriber/OrderedTabSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems 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 EzSystems\EzPlatformAdminUi\Tab\Event\Subscriber;
  8. use EzSystems\EzPlatformAdminUi\Tab\Event\TabEvents;
  9. use EzSystems\EzPlatformAdminUi\Tab\Event\TabGroupEvent;
  10. use EzSystems\EzPlatformAdminUi\Tab\OrderedTabInterface;
  11. use EzSystems\EzPlatformAdminUi\Tab\TabInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * Reorders tabs according to their Order value (Tabs implementing OrderedTabInterface).
  15.  * Tabs without order specified are pushed to the end of the group.
  16.  *
  17.  * @see OrderedTabInterface
  18.  */
  19. class OrderedTabSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @return array
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             TabEvents::TAB_GROUP_PRE_RENDER => ['onTabGroupPreRender'],
  28.         ];
  29.     }
  30.     /**
  31.      * @param TabGroupEvent $tabGroupEvent
  32.      */
  33.     public function onTabGroupPreRender(TabGroupEvent $tabGroupEvent)
  34.     {
  35.         $tabGroup $tabGroupEvent->getData();
  36.         $tabs $tabGroup->getTabs();
  37.         $tabs $this->reorderTabs($tabs);
  38.         $tabGroup->setTabs($tabs);
  39.         $tabGroupEvent->setData($tabGroup);
  40.     }
  41.     /**
  42.      * @param TabInterface[] $tabs
  43.      *
  44.      * @return array
  45.      */
  46.     private function reorderTabs($tabs): array
  47.     {
  48.         $orderedTabs = [];
  49.         foreach ($tabs as $tab) {
  50.             if ($tab instanceof OrderedTabInterface) {
  51.                 $orderedTabs[$tab->getIdentifier()] = $tab;
  52.                 unset($tabs[$tab->getIdentifier()]);
  53.             }
  54.         }
  55.         uasort($orderedTabs, [$this'sortTabs']);
  56.         return array_merge($orderedTabs$tabs);
  57.     }
  58.     /**
  59.      * @param OrderedTabInterface $tab1
  60.      * @param OrderedTabInterface $tab2
  61.      *
  62.      * @return int
  63.      */
  64.     private function sortTabs(OrderedTabInterface $tab1OrderedTabInterface $tab2): int
  65.     {
  66.         return $tab1->getOrder() <=> $tab2->getOrder();
  67.     }
  68. }