vendor/ibexa/admin-ui/src/lib/Tab/Event/Subscriber/OrderedTabSubscriber.php line 37

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