diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 11ef8f679a..f73dce81ea 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -8165,6 +8165,11 @@ parameters: count: 1 path: src/lib/Menu/UserPasswordChangeRightSidebarBuilder.php + - + message: "#^Cannot call method warning\\(\\) on Psr\\\\Log\\\\LoggerInterface\\|null\\.$#" + count: 1 + path: src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php + - message: "#^Method Ibexa\\\\AdminUi\\\\Menu\\\\UserSetting\\\\UserSettingUpdateRightSidebarBuilder\\:\\:createStructure\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/bundle/Resources/views/themes/admin/account/settings/update.html.twig b/src/bundle/Resources/views/themes/admin/account/settings/update.html.twig index 1a98ba6fd7..4333e5b24a 100644 --- a/src/bundle/Resources/views/themes/admin/account/settings/update.html.twig +++ b/src/bundle/Resources/views/themes/admin/account/settings/update.html.twig @@ -6,7 +6,13 @@ {% block body_class %}ibexa-user-settings-update-view{% endblock %} {% block header %} - {% set user_setting_update_sidebar_right = knp_menu_get('ezplatform_admin_ui.menu.user_setting_update.sidebar_right', [], {'user_setting': user_setting}) %} + {% set route = app.request.query.get('route') %} + {% set route_parameters = app.request.query.get('routeParameters') ?? [] %} + {% set user_setting_update_sidebar_right = knp_menu_get('ezplatform_admin_ui.menu.user_setting_update.sidebar_right', [], { + 'route': route, + 'route_parameters': route_parameters, + 'user_setting': user_setting, + }) %} {% include '@ibexadesign/ui/edit_header.html.twig' with { action_name: 'list.action_name'|trans({'%action%': title|lower})|desc('Editing %action%'), diff --git a/src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php b/src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php index 63cb8c59e6..1d5abce12f 100644 --- a/src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php +++ b/src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php @@ -14,7 +14,13 @@ use JMS\TranslationBundle\Model\Message; use JMS\TranslationBundle\Translation\TranslationContainerInterface; use Knp\Menu\ItemInterface; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Routing\Exception\ExceptionInterface as RouteExceptionInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -22,8 +28,10 @@ * * @see https://symfony.com/doc/current/bundles/KnpMenuBundle/menu_builder_service.html */ -class UserSettingUpdateRightSidebarBuilder extends AbstractBuilder implements TranslationContainerInterface +class UserSettingUpdateRightSidebarBuilder extends AbstractBuilder implements TranslationContainerInterface, LoggerAwareInterface { + use LoggerAwareTrait; + /* Menu items */ public const ITEM__SAVE = 'user_setting_update__sidebar_right__save'; public const ITEM__SAVE_AND_EDIT = 'user_setting_update__sidebar_right__save_end_edit'; @@ -32,14 +40,20 @@ class UserSettingUpdateRightSidebarBuilder extends AbstractBuilder implements Tr /** @var \Symfony\Contracts\Translation\TranslatorInterface */ private $translator; + private UrlGeneratorInterface $urlGenerator; + public function __construct( MenuItemFactory $factory, EventDispatcherInterface $eventDispatcher, - TranslatorInterface $translator + TranslatorInterface $translator, + UrlGeneratorInterface $urlGenerator, + ?LoggerInterface $logger = null ) { parent::__construct($factory, $eventDispatcher); $this->translator = $translator; + $this->urlGenerator = $urlGenerator; + $this->logger = $logger ?? new NullLogger(); } /** @@ -61,6 +75,12 @@ public function createStructure(array $options): ItemInterface { /** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */ $menu = $this->factory->createItem('root'); + $route = $options['route'] ?? 'ibexa.user_settings.list'; + $routeParameters = $options['route_parameters'] ?? []; + if (!$this->routeExists($route, $routeParameters)) { + $route = 'ibexa.user_settings.list'; + $routeParameters = []; + } $saveItem = $this->createMenuItem( self::ITEM__SAVE, @@ -69,7 +89,7 @@ public function createStructure(array $options): ItemInterface 'class' => 'ibexa-btn--trigger', 'data-click' => '#user_setting_update_update', ], - ] + ], ); $saveItem->addChild( @@ -90,7 +110,8 @@ public function createStructure(array $options): ItemInterface self::ITEM__CANCEL => $this->createMenuItem( self::ITEM__CANCEL, [ - 'route' => 'ibexa.user_settings.list', + 'route' => $route, + 'routeParameters' => $routeParameters, ] ), ]); @@ -109,6 +130,27 @@ public static function getTranslationMessages(): array (new Message(self::ITEM__CANCEL, 'ibexa_menu'))->setDesc('Discard'), ]; } + + /** + * @param array $routeParameters + */ + private function routeExists(string $route, array $routeParameters): bool + { + try { + $this->urlGenerator->generate($route, $routeParameters); + + return true; + } catch (RouteExceptionInterface $e) { + $this->logger->warning( + sprintf('Invalid route in query. %s.', $e->getMessage()), + [ + 'exception' => $e, + ], + ); + } + + return false; + } } class_alias(UserSettingUpdateRightSidebarBuilder::class, 'EzSystems\EzPlatformAdminUi\Menu\UserSetting\UserSettingUpdateRightSidebarBuilder');