Skip to content
Snippets Groups Projects
Commit 6be8f3de authored by catch's avatar catch
Browse files

Issue #3501727 by sayco, oily, berdir, catch, borisson_, longwave: Try to...

Issue #3501727 by sayco, oily, berdir, catch, borisson_, longwave: Try to simplify  checks in AdminNegotiator
parent d9286729
No related branches found
No related tags found
3 merge requests!11197Issue #3506427 by eduardo morales alberti: Remove responsive_image.ajax from hook,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!10223132456: Fix issue where views instances are emptied before an ajax request is complete
Pipeline #418064 passed with warnings
Pipeline: drupal

#418068

    ...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
    namespace Drupal\user\Theme; namespace Drupal\user\Theme;
    use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
    use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
    use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Routing\AdminContext; use Drupal\Core\Routing\AdminContext;
    use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
    ...@@ -13,6 +14,12 @@ ...@@ -13,6 +14,12 @@
    * Sets the active theme on admin pages. * Sets the active theme on admin pages.
    */ */
    class AdminNegotiator implements ThemeNegotiatorInterface { class AdminNegotiator implements ThemeNegotiatorInterface {
    use DeprecatedServicePropertyTrait;
    /**
    * The service properties that should raise a deprecation error.
    */
    private array $deprecatedProperties = ['entityTypeManager' => 'entity_type.manager'];
    /** /**
    * The current user. * The current user.
    ...@@ -28,13 +35,6 @@ class AdminNegotiator implements ThemeNegotiatorInterface { ...@@ -28,13 +35,6 @@ class AdminNegotiator implements ThemeNegotiatorInterface {
    */ */
    protected $configFactory; protected $configFactory;
    /**
    * The entity type manager.
    *
    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
    */
    protected $entityTypeManager;
    /** /**
    * The route admin context to determine whether a route is an admin one. * The route admin context to determine whether a route is an admin one.
    * *
    ...@@ -49,23 +49,29 @@ class AdminNegotiator implements ThemeNegotiatorInterface { ...@@ -49,23 +49,29 @@ class AdminNegotiator implements ThemeNegotiatorInterface {
    * The current user. * The current user.
    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
    * The config factory. * The config factory.
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Routing\AdminContext|EntityTypeManagerInterface $admin_context
    * The entity type manager.
    * @param \Drupal\Core\Routing\AdminContext $admin_context
    * The route admin context to determine whether the route is an admin one. * The route admin context to determine whether the route is an admin one.
    */ */
    public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, AdminContext $admin_context) { public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, AdminContext|EntityTypeManagerInterface $admin_context) {
    $this->user = $user; $this->user = $user;
    $this->configFactory = $config_factory; $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->adminContext = $admin_context; if ($admin_context instanceof EntityTypeManagerInterface) {
    $deprecated_service_name = EntityTypeManagerInterface::class;
    @trigger_error("Passing the $deprecated_service_name (entity_type.manager service) to AdminNegotiator is deprecated in drupal:11.2.0 and will be removed in drupal:12.0.0. There is no replacement for this service, as it is not used. See https://www.drupal.org/project/drupal/issues/3501727", E_USER_DEPRECATED);
    $this->adminContext = \Drupal::service('router.admin_context');
    }
    else {
    $this->adminContext = $admin_context;
    }
    } }
    /** /**
    * {@inheritdoc} * {@inheritdoc}
    */ */
    public function applies(RouteMatchInterface $route_match) { public function applies(RouteMatchInterface $route_match) {
    return ($this->entityTypeManager->hasHandler('user_role', 'storage') && $this->user->hasPermission('view the administration theme') && $this->adminContext->isAdminRoute($route_match->getRouteObject())); $is_admin_route = $this->adminContext->isAdminRoute($route_match->getRouteObject());
    return $is_admin_route && $this->user->hasPermission('view the administration theme');
    } }
    /** /**
    ......
    ...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
    namespace Drupal\Tests\user\Unit\Theme; namespace Drupal\Tests\user\Unit\Theme;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Routing\AdminContext; use Drupal\Core\Routing\AdminContext;
    use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatch;
    use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
    ...@@ -25,9 +24,8 @@ class AdminNegotiatorTest extends UnitTestCase { ...@@ -25,9 +24,8 @@ class AdminNegotiatorTest extends UnitTestCase {
    public function testDetermineActiveTheme($admin_theme, $expected): void { public function testDetermineActiveTheme($admin_theme, $expected): void {
    $user = $this->prophesize(AccountInterface::class); $user = $this->prophesize(AccountInterface::class);
    $config_factory = $this->getConfigFactoryStub(['system.theme' => ['admin' => $admin_theme]]); $config_factory = $this->getConfigFactoryStub(['system.theme' => ['admin' => $admin_theme]]);
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $admin_context = $this->prophesize(AdminContext::class); $admin_context = $this->prophesize(AdminContext::class);
    $negotiator = new AdminNegotiator($user->reveal(), $config_factory, $entity_type_manager->reveal(), $admin_context->reveal()); $negotiator = new AdminNegotiator($user->reveal(), $config_factory, $admin_context->reveal());
    $route_match = $this->prophesize(RouteMatch::class); $route_match = $this->prophesize(RouteMatch::class);
    $this->assertSame($expected, $negotiator->determineActiveTheme($route_match->reveal())); $this->assertSame($expected, $negotiator->determineActiveTheme($route_match->reveal()));
    } }
    ......
    ...@@ -39,7 +39,7 @@ services: ...@@ -39,7 +39,7 @@ services:
    arguments: ['@current_user', '@entity_type.manager', '@datetime.time'] arguments: ['@current_user', '@entity_type.manager', '@datetime.time']
    theme.negotiator.admin_theme: theme.negotiator.admin_theme:
    class: Drupal\user\Theme\AdminNegotiator class: Drupal\user\Theme\AdminNegotiator
    arguments: ['@current_user', '@config.factory', '@entity_type.manager', '@router.admin_context'] arguments: ['@current_user', '@config.factory', '@router.admin_context']
    tags: tags:
    - { name: theme_negotiator, priority: -40 } - { name: theme_negotiator, priority: -40 }
    user.auth: user.auth:
    ......
    ...@@ -335,7 +335,7 @@ protected function testLogin(): void { ...@@ -335,7 +335,7 @@ protected function testLogin(): void {
    $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
    $expected = [ $expected = [
    'QueryCount' => 17, 'QueryCount' => 17,
    'CacheGetCount' => 85, 'CacheGetCount' => 84,
    'CacheSetCount' => 1, 'CacheSetCount' => 1,
    'CacheDeleteCount' => 1, 'CacheDeleteCount' => 1,
    'CacheTagChecksumCount' => 1, 'CacheTagChecksumCount' => 1,
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment