You are here

class MicrositeCacheContext 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

Defines a class for a microsite cache context.

Hierarchy

Expanded class hierarchy of MicrositeCacheContext

1 string reference to 'MicrositeCacheContext'
entity_hierarchy_microsite.services.yml in modules/entity_hierarchy_microsite/entity_hierarchy_microsite.services.yml
modules/entity_hierarchy_microsite/entity_hierarchy_microsite.services.yml
1 service uses MicrositeCacheContext
cache_context.entity_hierarchy_microsite in modules/entity_hierarchy_microsite/entity_hierarchy_microsite.services.yml
Drupal\entity_hierarchy_microsite\Cache\MicrositeCacheContext

File

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

Namespace

Drupal\entity_hierarchy_microsite\Cache
View source
class MicrositeCacheContext implements CacheContextInterface {
  const NOT_A_MICROSITE = -1;

  /**
   * Lookup.
   *
   * @var \Drupal\entity_hierarchy_microsite\ChildOfMicrositeLookupInterface
   */
  private $childOfMicrositeLookup;

  /**
   * Route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  private $routeMatch;

  /**
   * Parent candidate.
   *
   * @var \Drupal\entity_hierarchy\Information\ParentCandidateInterface
   */
  private $parentCandidate;

  /**
   * Constructs a new MicrositeCacheContext.
   *
   * @param \Drupal\entity_hierarchy_microsite\ChildOfMicrositeLookupInterface $childOfMicrositeLookup
   *   Lookup.
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   Route match.
   * @param \Drupal\entity_hierarchy\Information\ParentCandidateInterface $parentCandidate
   *   Parent candidate.
   */
  public function __construct(ChildOfMicrositeLookupInterface $childOfMicrositeLookup, RouteMatchInterface $routeMatch, ParentCandidateInterface $parentCandidate) {
    $this->childOfMicrositeLookup = $childOfMicrositeLookup;
    $this->routeMatch = $routeMatch;
    $this->parentCandidate = $parentCandidate;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return 'Microsite ID';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MicrositeCacheContext::$childOfMicrositeLookup private property Lookup.
MicrositeCacheContext::$parentCandidate private property Parent candidate.
MicrositeCacheContext::$routeMatch private property Route match.
MicrositeCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
MicrositeCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
MicrositeCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
MicrositeCacheContext::NOT_A_MICROSITE constant
MicrositeCacheContext::__construct public function Constructs a new MicrositeCacheContext.