WIP: Implement menu/navigation
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Menu;
|
||||
|
||||
use App\Entity\User;
|
||||
use Knp\Menu\FactoryInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
abstract class AbstractMenuBuilder
|
||||
{
|
||||
public function __construct(
|
||||
protected FactoryInterface $factory,
|
||||
protected Security $security,
|
||||
protected RequestStack $requestStack,
|
||||
protected TranslatorInterface $translator
|
||||
) {
|
||||
}
|
||||
|
||||
protected function assertMobile(array $options): bool
|
||||
{
|
||||
return isset($options['mobile']) && true === (bool) $options['mobile'];
|
||||
}
|
||||
|
||||
protected function createRootElement(string $class): ItemInterface
|
||||
{
|
||||
return $this
|
||||
->factory->createItem('root')
|
||||
->setChildrenAttribute('class', $class)
|
||||
;
|
||||
}
|
||||
|
||||
protected function getDefaultRouteParameters(string $parameter = 'id', string $default = '0'): array
|
||||
{
|
||||
return [$parameter => $this->requestStack->getMainRequest()->get($parameter, $default)];
|
||||
}
|
||||
|
||||
protected function addLogoutItem(ItemInterface $menu, bool $isMobile = false): void
|
||||
{
|
||||
$token = $this->security->getToken();
|
||||
|
||||
if ($token instanceof SwitchUserToken) {
|
||||
/** @var User $originalUser */
|
||||
$originalUser = $token->getOriginalToken()->getUser();
|
||||
$menu
|
||||
->addChild('Logout', [
|
||||
'route' => $originalUser->getDefaultRoute(),
|
||||
'routeParameters' => ['_switch_user' => '_exit'],
|
||||
])
|
||||
->setLinkAttributes([
|
||||
'title' => 'zurück',
|
||||
'class' => 'icon-link',
|
||||
])
|
||||
->setExtra('icon', 'logout')
|
||||
->setExtra('icon_only', false === $isMobile)
|
||||
;
|
||||
} else {
|
||||
$menu
|
||||
->addChild('Logout', ['route' => 'app_security_logout'])
|
||||
->setLinkAttributes([
|
||||
'title' => 'Logout',
|
||||
'class' => 'icon-link',
|
||||
])
|
||||
->setExtra('icon', 'logout')
|
||||
->setExtra('icon_only', false === $isMobile)
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user