From b012b7f1db6fccb43400c447181d79e1479fffec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Thu, 15 Aug 2024 22:41:04 +0200 Subject: [PATCH] Better way of reordering menu items --- src/EventSubscriber/AddLinkToPeakSubscriber.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/EventSubscriber/AddLinkToPeakSubscriber.php b/src/EventSubscriber/AddLinkToPeakSubscriber.php index 14de90a..0b9acbe 100644 --- a/src/EventSubscriber/AddLinkToPeakSubscriber.php +++ b/src/EventSubscriber/AddLinkToPeakSubscriber.php @@ -12,6 +12,8 @@ final class AddLinkToPeakSubscriber implements EventSubscriberInterface { + private const MENU_ITEM_KEY = 'view_order_in_peak'; + public function __construct(private readonly bool $testEnvironment) { } @@ -41,7 +43,7 @@ public function addLink(OrderShowMenuBuilderEvent $event): void $menu = $event->getMenu(); $menu - ->addChild('view_order_in_peak', [ + ->addChild(self::MENU_ITEM_KEY, [ 'uri' => sprintf('https://app%s.peakwms.com/dialog/orderOverview/%d/details', $this->testEnvironment ? '-test' : '', $peakOrderId), ]) ->setAttribute('type', 'link') @@ -50,6 +52,12 @@ public function addLink(OrderShowMenuBuilderEvent $event): void ->setLabelAttribute('color', 'blue') ; - $menu->reorderChildren(['view_order_in_peak', 'order_history', 'cancel']); + $sort = array_keys($menu->getChildren()); + array_unshift($sort, self::MENU_ITEM_KEY); + + try { + $event->getMenu()->reorderChildren($sort); + } catch (\InvalidArgumentException) { + } } }