From 46ff3bb2b3605a92b3effd7a118f8d96c3506030 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 26 Nov 2016 22:30:04 +0000
Subject: [PATCH] Issue #2829588 by damiankloip, Fabianx, markcarver, dawehner,
 catch: Provide a "url.path.is_front" cache context to allow contributed
 themes to move global "is_front" variable back

---
 core/core.services.yml                        |  5 ++
 .../Cache/Context/IsFrontPathCacheContext.php | 52 +++++++++++++++++++
 .../Context/IsFrontPathCacheContextTest.php   | 46 ++++++++++++++++
 3 files changed, 103 insertions(+)
 create mode 100644 core/lib/Drupal/Core/Cache/Context/IsFrontPathCacheContext.php
 create mode 100644 core/tests/Drupal/Tests/Core/Cache/Context/IsFrontPathCacheContextTest.php

diff --git a/core/core.services.yml b/core/core.services.yml
index 5c42b23e0558..5b0de757832a 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -93,6 +93,11 @@ services:
     arguments: ['@request_stack']
     tags:
       - { name: cache.context }
+  cache_context.url.path.is_front:
+    class: Drupal\Core\Cache\Context\IsFrontPathCacheContext
+    arguments: ['@path.matcher']
+    tags:
+      - { name: cache.context }
   cache_context.url.query_args:
     class: Drupal\Core\Cache\Context\QueryArgsCacheContext
     arguments: ['@request_stack']
diff --git a/core/lib/Drupal/Core/Cache/Context/IsFrontPathCacheContext.php b/core/lib/Drupal/Core/Cache/Context/IsFrontPathCacheContext.php
new file mode 100644
index 000000000000..141b13a63f10
--- /dev/null
+++ b/core/lib/Drupal/Core/Cache/Context/IsFrontPathCacheContext.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Drupal\Core\Cache\Context;
+
+use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Path\PathMatcherInterface;
+
+/**
+ * Defines a cache context for whether the URL is the front page of the site.
+ *
+ * Cache context ID: 'url.path.is_front'.
+ */
+class IsFrontPathCacheContext implements CacheContextInterface {
+
+  /**
+   * @var \Drupal\Core\Path\PathMatcherInterface
+   */
+  protected $pathMatcher;
+
+  /**
+   * Constructs an IsFrontPathCacheContext object.
+   *
+   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
+   */
+  public function __construct(PathMatcherInterface $path_matcher) {
+    $this->pathMatcher = $path_matcher;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getLabel() {
+    return t('Is front page');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getContext() {
+    return 'is_front.' . (int) $this->pathMatcher->isFrontPage();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheableMetadata() {
+    $metadata = new CacheableMetadata();
+    $metadata->addCacheTags(['config:system.site']);
+    return $metadata;
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Cache/Context/IsFrontPathCacheContextTest.php b/core/tests/Drupal/Tests/Core/Cache/Context/IsFrontPathCacheContextTest.php
new file mode 100644
index 000000000000..5ccb18566f3b
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Cache/Context/IsFrontPathCacheContextTest.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Drupal\Tests\Core\Cache\Context;
+
+use Drupal\Core\Cache\Context\IsFrontPathCacheContext;
+use Drupal\Core\Path\PathMatcherInterface;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Cache\Context\IsFrontPathCacheContext
+ * @group Cache
+ */
+class IsFrontPathCacheContextTest extends UnitTestCase {
+
+  /**
+   * @covers ::getContext
+   */
+  public function testGetContextFront() {
+    $cache_context = new IsFrontPathCacheContext($this->createPathMatcher(TRUE)->reveal());
+    $this->assertSame('is_front.1', $cache_context->getContext());
+  }
+
+  /**
+   * @covers ::getContext
+   */
+  public function testGetContextNotFront() {
+    $cache_context = new IsFrontPathCacheContext($this->createPathMatcher(FALSE)->reveal());
+    $this->assertSame('is_front.0', $cache_context->getContext());
+  }
+
+  /**
+   * Creates a PathMatcherInterface prophecy.
+   *
+   * @param bool $is_front
+   *
+   * @return \Prophecy\Prophecy\ObjectProphecy
+   */
+  protected function createPathMatcher($is_front) {
+    $path_matcher = $this->prophesize(PathMatcherInterface::class);
+    $path_matcher->isFrontPage()
+      ->willReturn($is_front);
+
+    return $path_matcher;
+  }
+
+}
-- 
GitLab