From ee9dacfbb601cff5b2e6df7e242857355e599bb6 Mon Sep 17 00:00:00 2001 From: webchick Date: Wed, 19 Jul 2017 14:12:03 -0700 Subject: [PATCH] Issue #2784853 by Wim Leers, tedbow: Determine when Outside In library should be loaded: piggyback on contextual_toolbar() --- core/modules/outside_in/outside_in.module | 13 ++-- .../outside_in/outside_in.services.yml | 10 --- .../Cache/Context/OutsideInCacheContext.php | 54 --------------- .../outside_in/src/OutsideInManager.php | 66 ------------------- .../src/OutsideInManagerInterface.php | 18 ----- .../tests/src/Unit/OutsideInManagerTest.php | 57 ---------------- 6 files changed, 8 insertions(+), 210 deletions(-) delete mode 100644 core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php delete mode 100644 core/modules/outside_in/src/OutsideInManager.php delete mode 100644 core/modules/outside_in/src/OutsideInManagerInterface.php delete mode 100644 core/modules/outside_in/tests/src/Unit/OutsideInManagerTest.php diff --git a/core/modules/outside_in/outside_in.module b/core/modules/outside_in/outside_in.module index 6673fa37185..69517b99196 100644 --- a/core/modules/outside_in/outside_in.module +++ b/core/modules/outside_in/outside_in.module @@ -101,8 +101,7 @@ function outside_in_entity_type_build(array &$entity_types) { */ function outside_in_preprocess_block(&$variables) { // The main system block does not contain the block contextual links. - $variables['#cache']['contexts'][] = 'outside_in_is_applied'; - if ($variables['plugin_id'] !== 'system_main_block' && \Drupal::service('outside_in.manager')->isApplicable()) { + if ($variables['plugin_id'] !== 'system_main_block') { // Add class and attributes to all blocks to allow Javascript to target. $variables['attributes']['class'][] = 'outside-in-editable'; $variables['attributes']['data-drupal-outsidein'] = 'editable'; @@ -112,11 +111,15 @@ function outside_in_preprocess_block(&$variables) { /** * Implements hook_toolbar_alter(). * - * Includes outside_library if Edit link is in toolbar. + * Alters the 'contextual' toolbar tab if it exists (meaning the user is allowed + * to use contextual links) and if they can administer blocks. + * + * @todo Remove the "administer blocks" requirement in https://www.drupal.org/node/2822965 + * @see contextual_toolbar() */ function outside_in_toolbar_alter(&$items) { - $items['contextual']['#cache']['contexts'][] = 'outside_in_is_applied'; - if (isset($items['contextual']['tab']) && \Drupal::service('outside_in.manager')->isApplicable()) { + $items['contextual']['#cache']['contexts'][] = 'user.permissions'; + if (isset($items['contextual']['tab']) && \Drupal::currentUser()->hasPermission('administer blocks')) { $items['contextual']['#weight'] = -1000; $items['contextual']['#attached']['library'][] = 'outside_in/drupal.outside_in'; $items['contextual']['tab']['#attributes']['data-drupal-outsidein'] = 'toggle'; diff --git a/core/modules/outside_in/outside_in.services.yml b/core/modules/outside_in/outside_in.services.yml index a53ce2938dd..ce8214697a1 100644 --- a/core/modules/outside_in/outside_in.services.yml +++ b/core/modules/outside_in/outside_in.services.yml @@ -4,13 +4,3 @@ services: arguments: ['@title_resolver', '@renderer'] tags: - { name: render.main_content_renderer, format: drupal_dialog.off_canvas } - - outside_in.manager: - class: Drupal\outside_in\OutsideInManager - arguments: ['@router.admin_context', '@current_route_match', '@current_user'] - - cache_context.outside_in_is_applied: - class: Drupal\outside_in\Cache\Context\OutsideInCacheContext - arguments: ['@outside_in.manager'] - tags: - - { name: cache.context} diff --git a/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php b/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php deleted file mode 100644 index 4a24671d014..00000000000 --- a/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php +++ /dev/null @@ -1,54 +0,0 @@ -outsideInManager = $outside_in_manager; - } - - /** - * {@inheritdoc} - */ - public static function getLabel() { - return t('Settings Tray'); - } - - /** - * {@inheritdoc} - */ - public function getContext() { - return $this->outsideInManager->isApplicable() ? '1' : '0'; - } - - /** - * {@inheritdoc} - */ - public function getCacheableMetadata() { - return new CacheableMetadata(); - } - -} diff --git a/core/modules/outside_in/src/OutsideInManager.php b/core/modules/outside_in/src/OutsideInManager.php deleted file mode 100644 index 666a93837be..00000000000 --- a/core/modules/outside_in/src/OutsideInManager.php +++ /dev/null @@ -1,66 +0,0 @@ -adminContext = $admin_context; - $this->routeMatch = $route_match; - $this->account = $account; - } - - /** - * {@inheritdoc} - */ - public function isApplicable() { - // Remove on Admin routes. - $is_admin_route = $this->adminContext->isAdminRoute(); - - // Remove on Block Demo page. - $is_admin_demo_route = $this->routeMatch->getRouteName() === 'block.admin_demo'; - - // @todo Check if there is actually a different admin theme. - // https://www.drupal.org/node/2784853 - return $this->account->hasPermission('administer blocks') && !$is_admin_route && !$is_admin_demo_route; - } - -} diff --git a/core/modules/outside_in/src/OutsideInManagerInterface.php b/core/modules/outside_in/src/OutsideInManagerInterface.php deleted file mode 100644 index 684adb3f0df..00000000000 --- a/core/modules/outside_in/src/OutsideInManagerInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -prophesize(AdminContext::class); - $admin_context->isAdminRoute()->willReturn($is_admin_route); - - $route_match = $this->prophesize(RouteMatchInterface::class); - $route_match->getRouteName()->willReturn($route_name); - - $account = $this->prophesize(AccountInterface::class); - $account->hasPermission('administer blocks')->willReturn($has_permission); - - $outside_in_manager = new OutsideInManager($admin_context->reveal(), $route_match->reveal(), $account->reveal()); - - $this->assertSame($expected, $outside_in_manager->isApplicable()); - } - - /** - * Data provider for ::testIsApplicable(). - */ - public function providerTestIsApplicable() { - $data = []; - - // Passing combination. - $data[] = [FALSE, 'the_route_name', TRUE, TRUE]; - - // Failing combinations. - $data[] = [TRUE, 'the_route_name', TRUE, FALSE]; - $data[] = [TRUE, 'the_route_name', FALSE, FALSE]; - $data[] = [TRUE, 'block.admin_demo', TRUE, FALSE]; - $data[] = [TRUE, 'block.admin_demo', FALSE, FALSE]; - $data[] = [FALSE, 'the_route_name', FALSE, FALSE]; - $data[] = [FALSE, 'block.admin_demo', TRUE, FALSE]; - $data[] = [FALSE, 'block.admin_demo', FALSE, FALSE]; - - return $data; - } - -}