contextRepository example

/** * Gets the available contexts for a given entity. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity. * * @return \Drupal\Core\Plugin\Context\ContextInterface[] * An array of context objects for a given entity. */
  protected function getContextsForEntity(FieldableEntityInterface $entity) {
    $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    return [
      'view_mode' => new Context(ContextDefinition::create('string')$this->getMode()),
      'entity' => EntityContext::fromEntity($entity),
      'display' => EntityContext::fromEntity($this),
    ] + $this->contextRepository()->getRuntimeContexts($available_context_ids);
  }

  /** * {@inheritdoc} * * @todo Move this upstream in https://www.drupal.org/node/2939931. */
/** * Returns all populated contexts, both global and section-storage-specific. * * @param \Drupal\layout_builder\SectionStorageInterface $section_storage * The section storage. * * @return \Drupal\Core\Plugin\Context\ContextInterface[] * The array of context objects. */
  protected function getPopulatedContexts(SectionStorageInterface $section_storage): array {
    // Get all known globally available contexts IDs.     $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    // Filter to those that are populated.     $contexts = array_filter($this->contextRepository()->getRuntimeContexts($available_context_ids)function DContextInterface $context) {
      return $context->hasContextValue();
    });

    // Add in the per-section_storage contexts.     $contexts += $section_storage->getContextsDuringPreview();
    return $contexts;
  }

}
Home | Imprint | This part of the site doesn't use cookies.