You are here

class NodeRouteContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext
  2. 10 core/modules/node/src/ContextProvider/NodeRouteContext.php \Drupal\node\ContextProvider\NodeRouteContext

Sets the current node as a context on node routes.

Hierarchy

Expanded class hierarchy of NodeRouteContext

1 string reference to 'NodeRouteContext'
node.services.yml in core/modules/node/node.services.yml
core/modules/node/node.services.yml
1 service uses NodeRouteContext
node.node_route_context in core/modules/node/node.services.yml
Drupal\node\ContextProvider\NodeRouteContext

File

core/modules/node/src/ContextProvider/NodeRouteContext.php, line 17

Namespace

Drupal\node\ContextProvider
View source
class NodeRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * The route match object.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new NodeRouteContext.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $result = [];
    $context_definition = EntityContextDefinition::create('node')
      ->setRequired(FALSE);
    $value = NULL;
    if ($route_object = $this->routeMatch
      ->getRouteObject()) {
      $route_contexts = $route_object
        ->getOption('parameters');

      // Check for a node revision parameter first.
      if (isset($route_contexts['node_revision']) && ($revision = $this->routeMatch
        ->getParameter('node_revision'))) {
        $value = $revision;
      }
      elseif (isset($route_contexts['node']) && ($node = $this->routeMatch
        ->getParameter('node'))) {
        $value = $node;
      }
      elseif (isset($route_contexts['node_preview']) && ($node = $this->routeMatch
        ->getParameter('node_preview'))) {
        $value = $node;
      }
      elseif ($this->routeMatch
        ->getRouteName() == 'node.add') {
        $node_type = $this->routeMatch
          ->getParameter('node_type');
        $value = Node::create([
          'type' => $node_type
            ->id(),
        ]);
      }
    }
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'route',
    ]);
    $context = new Context($context_definition, $value);
    $context
      ->addCacheableDependency($cacheability);
    $result['node'] = $context;
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('node', $this
      ->t('Node from URL'));
    return [
      'node' => $context,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeRouteContext::$routeMatch protected property The route match object.
NodeRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
NodeRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
NodeRouteContext::__construct public function Constructs a new NodeRouteContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.