Implemented abstract entity menu class

This commit is contained in:
Kevin Frantz 2018-10-04 21:25:35 +02:00
parent 00cd03f43d
commit cfcc498d4e
2 changed files with 71 additions and 58 deletions

View File

@ -0,0 +1,69 @@
<?php
namespace App\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Translation\TranslatorInterface;
/**
*
* @author kevinfrantz
*
*/
abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
protected $translator;
const FORMAT_TYPES = [
'html',
'json',
'xml'
];
public function __construct(TranslatorInterface $translator)
{
$this->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');
}
}

View File

@ -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 [