diff --git a/core/modules/outside_in/outside_in.module b/core/modules/outside_in/outside_in.module index 83c68b6..d944565 100644 --- a/core/modules/outside_in/outside_in.module +++ b/core/modules/outside_in/outside_in.module @@ -76,36 +76,21 @@ 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 (_outside_in_apply_on_current_page() && $variables['plugin_id'] !== 'system_main_block') { + $variables['#cache']['contexts'][] = 'outside_in_is_applied'; + if (\Drupal::service('outside_in.info')->isApplied() && $variables['plugin_id'] !== 'system_main_block') { // Add class to all blocks to allow Javascript to target. $variables['attributes']['class'][] = 'outside-in-editable'; } } /** - * Determines if the Outside-In logic should be run on the current page. - * - * @return bool - * TRUE if the Outside-In logic should be run. - */ -function _outside_in_apply_on_current_page() { - // Remove on Admin routes. - $admin_route = \Drupal::service('router.admin_context')->isAdminRoute(); - // @todo Check if there is actually a different admin theme. - // Remove on Block Demo page. - $admin_demo = \Drupal::routeMatch()->getRouteName() === 'block.admin_demo'; - return \Drupal::currentUser()->hasPermission('administer blocks') && !$admin_route && !$admin_demo; -} - -/** * Implements hook_toolbar_alter(). * * Includes outside_library if Edit link is in toolbar. */ function outside_in_toolbar_alter(&$items) { - $items['contextual']['#cache']['contexts'][] = 'outside_in.is_applied'; - if (_outside_in_apply_on_current_page() && isset($items['contextual']['tab'])) { + $items['contextual']['#cache']['contexts'][] = 'outside_in_is_applied'; + if (\Drupal::service('outside_in.info')->isApplied() && isset($items['contextual']['tab'])) { $items['contextual']['#weight'] = -1000; $items['contextual']['#attached']['library'][] = 'outside_in/drupal.outside_in'; diff --git a/core/modules/outside_in/outside_in.services.yml b/core/modules/outside_in/outside_in.services.yml index 07dcbe6..47b2585 100644 --- a/core/modules/outside_in/outside_in.services.yml +++ b/core/modules/outside_in/outside_in.services.yml @@ -5,7 +5,12 @@ services: tags: - { name: render.main_content_renderer, format: drupal_offcanvas } - cache_context.outside_in.is_applied: + outside_in.info: + class: Drupal\outside_in\OutsideInInfo + 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.info'] 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 index c6d4c14..36b8fe3 100644 --- a/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php +++ b/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php @@ -4,15 +4,33 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\Context\CacheContextInterface; +use Drupal\outside_in\OutsideInInfoInterface; /** * Defines the OutsideInCacheContext service, for "Outside-In or not" caching. * - * Cache context ID: 'outside_in.is_applied'. + * Cache context ID: 'outside_in_is_applied'. */ class OutsideInCacheContext implements CacheContextInterface { /** + * The Outside-In info. + * + * @var \Drupal\outside_in\OutsideInInfoInterface + */ + protected $outsideInInfo; + + /** + * OutsideInCacheContext constructor. + * + * @param \Drupal\outside_in\OutsideInInfoInterface $outside_in_info + * The Outside-In info. + */ + public function __construct(OutsideInInfoInterface $outside_in_info) { + $this->outsideInInfo = $outside_in_info; + } + + /** * {@inheritdoc} */ public static function getLabel() { @@ -23,16 +41,14 @@ public static function getLabel() { * {@inheritdoc} */ public function getContext() { - return _outside_in_apply_on_current_page() ? '1' : '0'; + return $this->outsideInInfo->isApplied() ? '1' : '0'; } /** * {@inheritdoc} */ public function getCacheableMetadata() { - $cacheable_metadata = new CacheableMetadata(); - // @todo What goes here? - return $cacheable_metadata; + return new CacheableMetadata(); } } diff --git a/core/modules/outside_in/src/OutsideInInfo.php b/core/modules/outside_in/src/OutsideInInfo.php new file mode 100644 index 0000000..1af0a2a --- /dev/null +++ b/core/modules/outside_in/src/OutsideInInfo.php @@ -0,0 +1,65 @@ +adminContext = $admin_context; + $this->routeMatch = $route_match; + $this->account = $account; + } + + /** + * {@inheritdoc} + */ + public function isApplied() { + // Remove on Admin routes. + $admin_route = $this->adminContext->isAdminRoute(); + + // Remove on Block Demo page. + $admin_demo = $this->routeMatch->getRouteName() === 'block.admin_demo'; + + // @todo Check if there is actually a different admin theme. + return $this->account->hasPermission('administer blocks') && !$admin_route && !$admin_demo; + } + +} diff --git a/core/modules/outside_in/src/OutsideInInfoInterface.php b/core/modules/outside_in/src/OutsideInInfoInterface.php new file mode 100644 index 0000000..bd18386 --- /dev/null +++ b/core/modules/outside_in/src/OutsideInInfoInterface.php @@ -0,0 +1,18 @@ +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_info = new OutsideInInfo($admin_context->reveal(), $route_match->reveal(), $account->reveal()); + + $this->assertSame($expected, $outside_in_info->isApplied()); + } + + public function providerTestIsApplied() { + $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; + } + +}