Node.php 2.14 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\node\Plugin\views\argument_default\Node.
6 7
 */

8
namespace Drupal\node\Plugin\views\argument_default;
9

10
use Drupal\Core\Routing\RouteMatchInterface;
11
use Drupal\views\Plugin\CacheablePluginInterface;
12
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
13
use Drupal\node\NodeInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15 16

/**
17
 * Default argument plugin to extract a node.
18 19
 *
 * This plugin actually has no options so it odes not need to do a great deal.
20
 *
21
 * @ViewsArgumentDefault(
aspilicious's avatar
aspilicious committed
22
 *   id = "node",
23 24 25
 *   title = @Translation("Content ID from URL")
 * )
 */
26
class Node extends ArgumentDefaultPluginBase implements CacheablePluginInterface {
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new Date 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')
    );
  }

66 67 68 69
  /**
   * {@inheritdoc}
   */
  public function getArgument() {
70
    if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
71 72 73
      return $node->id();
    }
  }
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88
  /**
   * {@inheritdoc}
   */
  public function isCacheable() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return ['cache.context.url'];
  }

89
}