Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Plugin / views / argument_default / User.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\argument_default;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Drupal\user\UserInterface;
12 use Drupal\node\NodeInterface;
13
14 /**
15  * Default argument plugin to extract a user from request.
16  *
17  * @ViewsArgumentDefault(
18  *   id = "user",
19  *   title = @Translation("User ID from route context")
20  * )
21  */
22 class User extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
23
24   /**
25    * The route match.
26    *
27    * @var \Drupal\Core\Routing\RouteMatchInterface
28    */
29   protected $routeMatch;
30
31   /**
32    * Constructs a new User instance.
33    *
34    * @param array $configuration
35    *   A configuration array containing information about the plugin instance.
36    * @param string $plugin_id
37    *   The plugin_id for the plugin instance.
38    * @param mixed $plugin_definition
39    *   The plugin implementation definition.
40    *
41    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
42    *   The route match.
43    */
44   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
45     parent::__construct($configuration, $plugin_id, $plugin_definition);
46
47     $this->routeMatch = $route_match;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
54     return new static(
55       $configuration,
56       $plugin_id,
57       $plugin_definition,
58       $container->get('current_route_match')
59     );
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   protected function defineOptions() {
66     $options = parent::defineOptions();
67     $options['user'] = ['default' => ''];
68
69     return $options;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
76     $form['user'] = [
77       '#type' => 'checkbox',
78       '#title' => $this->t('Also look for a node and use the node author'),
79       '#default_value' => $this->options['user'],
80     ];
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getArgument() {
87
88     // If there is a user object in the current route.
89     if ($user = $this->routeMatch->getParameter('user')) {
90       if ($user instanceof UserInterface) {
91         return $user->id();
92       }
93     }
94
95     // If option to use node author; and node in current route.
96     if (!empty($this->options['user']) && $node = $this->routeMatch->getParameter('node')) {
97       if ($node instanceof NodeInterface) {
98         return $node->getOwnerId();
99       }
100     }
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getCacheMaxAge() {
107     return Cache::PERMANENT;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function getCacheContexts() {
114     return ['url'];
115   }
116
117 }