core/core.services.yml | 3 ++- .../Core/Cache/MenuActiveTrailCacheContext.php | 28 +++++++--------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 13fc8d5..7ac81da 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -35,7 +35,8 @@ services: - { name: cache.context} cache_context.menu.active_trail: class: Drupal\Core\Cache\MenuActiveTrailCacheContext - arguments: ['@menu.active_trail'] + calls: + - [setContainer, ['@service_container']] tags: - { name: cache.context} cache_tags.invalidator: diff --git a/core/lib/Drupal/Core/Cache/MenuActiveTrailCacheContext.php b/core/lib/Drupal/Core/Cache/MenuActiveTrailCacheContext.php index 755b885..3ba4ad2 100644 --- a/core/lib/Drupal/Core/Cache/MenuActiveTrailCacheContext.php +++ b/core/lib/Drupal/Core/Cache/MenuActiveTrailCacheContext.php @@ -7,29 +7,15 @@ namespace Drupal\Core\Cache; -use Drupal\Core\Menu\MenuActiveTrailInterface; +use Symfony\Component\DependencyInjection\ContainerAware; /** * Defines the MenuActiveTrailCacheContext service. + * + * This class is container-aware to avoid initializing the 'menu.active_trail' + * service (and its dependencies) when it is not necessary. */ -class MenuActiveTrailCacheContext implements CalculatedCacheContextInterface { - - /** - * The active menu trail service. - * - * @var \Drupal\Core\Menu\MenuActiveTrailInterface - */ - protected $menuActiveTrail; - - /** - * Constructs a new MenuActiveTrailCacheContext service. - * - * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail - * The active menu trail service. - */ - public function __construct(MenuActiveTrailInterface $menu_active_trail) { - $this->menuActiveTrail = $menu_active_trail; - } +class MenuActiveTrailCacheContext extends ContainerAware implements CalculatedCacheContextInterface { /** * {@inheritdoc} @@ -42,7 +28,9 @@ public static function getLabel() { * {@inheritdoc} */ public function getContext($menu_name) { - return 'menu_trail.' . implode('|', $this->menuActiveTrail->getActiveTrailIds($menu_name)); + $active_trail = $this->container->get('menu.active_trail') + ->getActiveTrailIds($menu_name); + return 'menu_trail.' . implode('|', $active_trail); } }