You are here

public function MicrositeCacheContext::getContext in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 modules/entity_hierarchy_microsite/src/Cache/MicrositeCacheContext.php \Drupal\entity_hierarchy_microsite\Cache\MicrositeCacheContext::getContext()

Returns the string representation of the cache context.

A cache context service's name is used as a token (placeholder) cache key, and is then replaced with the string returned by this method.

Return value

string The string representation of the cache context.

Overrides CacheContextInterface::getContext

File

modules/entity_hierarchy_microsite/src/Cache/MicrositeCacheContext.php, line 66

Class

MicrositeCacheContext
Defines a class for a microsite cache context.

Namespace

Drupal\entity_hierarchy_microsite\Cache

Code

public function getContext() {
  $node = $this->routeMatch
    ->getParameter('node');
  if (!$node || !$node instanceof NodeInterface) {
    return self::NOT_A_MICROSITE;
  }
  if (!($fields = $this->parentCandidate
    ->getCandidateFields($node))) {
    return self::NOT_A_MICROSITE;
  }
  foreach ($fields as $field_name) {
    if ($microsites = $this->childOfMicrositeLookup
      ->findMicrositesForNodeAndField($node, $field_name)) {
      return implode(':', array_keys($microsites));
    }
  }
  return self::NOT_A_MICROSITE;
}