By damiankloip on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.3.x
Introduced in version:
8.3.0
Issue links:
Description:
A new cache context has been added to determine whether the current path is the front page and vary cache entries by this boolean type value. This can be used by using url.path.is_front in cache contexts.
// ...
$build['#cache']['contexts'][] = 'url.path.is_front';
Or e.g. in a preprocess function:
/**
* Implements hook_preprocess().
*/
mymodule_preprocess(&$variables) {
// Set a variable based on the path.matcher.
if (!isset($variables['is_front'])) {
try {
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
}
catch (\Exception $e) {
$variables['is_front'] = FALSE;
}
}
// Ensure the cache varies correctly.
$variables['#cache']['contexts'][] = 'url.path.is_front';
}
The code for this context can be found in the Drupal\Core\Cache\Context\IsFrontPathCacheContext class.
Impacts:
Module developers
Themers