Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

Node.php

Same filename in this branch
  1. 11.1.x vendor/phpunit/php-code-coverage/src/Report/Xml/Node.php
  2. 11.1.x vendor/phpstan/phpdoc-parser/src/Ast/Node.php
  3. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node.php
  4. 11.1.x vendor/twig/twig/src/Node/Node.php
  5. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/Node/Node.php
  6. 11.1.x core/modules/node/src/Plugin/migrate/source/d6/Node.php
  7. 11.1.x core/modules/node/src/Plugin/migrate/source/d7/Node.php
  8. 11.1.x core/modules/node/src/Plugin/views/field/Node.php
  9. 11.1.x core/modules/node/src/Plugin/views/wizard/Node.php
  10. 11.1.x core/modules/node/src/Entity/Node.php

Namespace

Drupal\node\Plugin\views\argument_default

File

core/modules/node/src/Plugin/views/argument_default/Node.php

View source
<?php

namespace Drupal\node\Plugin\views\argument_default;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsArgumentDefault;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Default argument plugin to extract a node.
 */
class Node extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
    
    /**
     * The route match.
     *
     * @var \Drupal\Core\Routing\RouteMatchInterface
     */
    protected $routeMatch;
    
    /**
     * Constructs a new Node instance.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin ID for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->routeMatch = $route_match;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('current_route_match'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getArgument() {
        // Get the node object from current route.
        $node = $this->routeMatch
            ->getParameter('node') ?? $this->routeMatch
            ->getParameter('node_preview');
        if ($node instanceof NodeInterface) {
            return $node->id();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() {
        return Cache::PERMANENT;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'url',
        ];
    }

}

Classes

Title Deprecated Summary
Node Default argument plugin to extract a node.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal