Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7044: Redirect after settings update #1173

Open
wants to merge 10 commits into
base: 4.6
Choose a base branch
from
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')|default(null) %}
Steveb-p marked this conversation as resolved.
Show resolved Hide resolved
{% set route_parameters = app.request.query.get('routeParameters')|default([]) %}
Steveb-p marked this conversation as resolved.
Show resolved Hide resolved
{% 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%'),
Expand Down
50 changes: 46 additions & 4 deletions src/lib/Menu/UserSetting/UserSettingUpdateRightSidebarBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
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\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* KnpMenuBundle Menu Builder service implementation for User Setting Edit contextual sidebar menu.
*
* @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';
Expand All @@ -32,14 +40,20 @@ class UserSettingUpdateRightSidebarBuilder extends AbstractBuilder implements Tr
/** @var \Symfony\Contracts\Translation\TranslatorInterface */
private $translator;

private RouterInterface $router;

public function __construct(
MenuItemFactory $factory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator
TranslatorInterface $translator,
RouterInterface $router,
?LoggerInterface $logger = null
) {
parent::__construct($factory, $eventDispatcher);

$this->translator = $translator;
$this->router = $router;
$this->logger = $logger ?? new NullLogger();
}

/**
Expand All @@ -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,
Expand All @@ -69,7 +89,7 @@ public function createStructure(array $options): ItemInterface
'class' => 'ibexa-btn--trigger',
'data-click' => '#user_setting_update_update',
],
]
],
);

$saveItem->addChild(
Expand All @@ -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,
Steveb-p marked this conversation as resolved.
Show resolved Hide resolved
'routeParameters' => $routeParameters,
]
),
]);
Expand All @@ -109,6 +130,27 @@ public static function getTranslationMessages(): array
(new Message(self::ITEM__CANCEL, 'ibexa_menu'))->setDesc('Discard'),
];
}

/**
* @param array<mixed> $routeParameters
*/
private function routeExists(string $route, array $routeParameters): bool
{
try {
$this->router->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');
Loading