From cfcc498d4e7c922b8ba1392ace0ab08c20fa41e2 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Thu, 4 Oct 2018 21:25:35 +0200 Subject: [PATCH] Implemented abstract entity menu class --- .../AbstractEntityMenuSubscriber.php | 69 +++++++++++++++++++ .../src/Subscriber/SourceMenuSubscriber.php | 60 +--------------- 2 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 application/src/Subscriber/AbstractEntityMenuSubscriber.php diff --git a/application/src/Subscriber/AbstractEntityMenuSubscriber.php b/application/src/Subscriber/AbstractEntityMenuSubscriber.php new file mode 100644 index 0000000..dcccd7e --- /dev/null +++ b/application/src/Subscriber/AbstractEntityMenuSubscriber.php @@ -0,0 +1,69 @@ +translator = $translator; + } + + protected function generateShowDropdown(ItemInterface $menu, Event $event,string $route): void + { + $dropdown = $menu->addChild($this->translator->trans('show'), [ + 'attributes' => [ + 'icon' => 'fas fa-eye', + 'dropdown' => 'true' + ] + ]); + foreach (self::FORMAT_TYPES as $format) { + $dropdown->addChild($format, [ + 'route' => $route, + 'routeParameters' => [ + 'id' => $this->getRequestId($event), + '_format' => $format + ], + 'attributes' => [ + 'icon' => 'fas fa-sign-out-alt', + 'divider_append' => true + ] + ]); + } + $dropdown->addChild($this->translator->trans('standard'), [ + 'route' => $route, + 'routeParameters' => [ + 'id' => $this->getRequestId($event) + ], + 'attributes' => [ + 'icon' => 'fas fa-sign-out-alt' + ] + ]); + } + + private function getRequestId(Event $event): int + { + return $event->getRequest()->getCurrentRequest()->attributes->get('id'); + } +} + diff --git a/application/src/Subscriber/SourceMenuSubscriber.php b/application/src/Subscriber/SourceMenuSubscriber.php index 40466b5..d58a518 100644 --- a/application/src/Subscriber/SourceMenuSubscriber.php +++ b/application/src/Subscriber/SourceMenuSubscriber.php @@ -2,30 +2,10 @@ namespace App\Subscriber; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; -use Symfony\Component\Translation\TranslatorInterface; use App\Event\Menu\Topbar\SourceMenuEvent; -use Knp\Menu\ItemInterface; -class SourceMenuSubscriber implements EventSubscriberInterface +class SourceMenuSubscriber extends AbstractEntityMenuSubscriber { - /** - * @var TokenStorageInterface - */ - private $tokenStorage; - - /** - * @var TranslatorInterface - */ - private $translator; - - public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator) - { - $this->tokenStorage = $tokenStorage; - $this->translator = $translator; - } - public function onSourceMenuConfigure(SourceMenuEvent $event): void { $menu = $event->getItem(); @@ -38,7 +18,7 @@ class SourceMenuSubscriber implements EventSubscriberInterface 'icon' => 'fas fa-edit', ], ]); - $this->generateSourceFormatDropdown($menu, $event); + $this->generateShowDropdown($menu, $event,'app_source_show'); $menu->addChild($this->translator->trans('node'), [ 'route' => 'app_source_node', 'routeParameters' => [ @@ -50,42 +30,6 @@ class SourceMenuSubscriber implements EventSubscriberInterface ]); } - private function generateSourceFormatDropdown(ItemInterface $menu, SourceMenuEvent $event): void - { - $dropdown = $menu->addChild($this->translator->trans('show'), [ - 'attributes' => [ - 'icon' => 'fas fa-eye', - 'dropdown' => 'true', - ], - ]); - foreach ([ - 'html', - 'json', - 'xml', - ] as $format) { - $dropdown->addChild($format, [ - 'route' => 'app_source_show', - 'routeParameters' => [ - 'id' => $event->getRequest()->getCurrentRequest()->attributes->get('id'), - '_format' => $format, - ], - 'attributes' => [ - 'icon' => 'fas fa-sign-out-alt', - 'divider_append' => true, - ], - ]); - } - $dropdown->addChild($this->translator->trans('standard'), [ - 'route' => 'app_source_show', - 'routeParameters' => [ - 'id' => $event->getRequest()->getCurrentRequest()->attributes->get('id'), - ], - 'attributes' => [ - 'icon' => 'fas fa-sign-out-alt', - ], - ]); - } - public static function getSubscribedEvents(): array { return [