3 namespace Drupal\user\Plugin\views\argument_default;
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;
15 * Default argument plugin to extract a user from request.
17 * @ViewsArgumentDefault(
19 * title = @Translation("User ID from route context")
22 class User extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
27 * @var \Drupal\Core\Routing\RouteMatchInterface
29 protected $routeMatch;
32 * Constructs a new User instance.
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.
41 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
44 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
45 parent::__construct($configuration, $plugin_id, $plugin_definition);
47 $this->routeMatch = $route_match;
53 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
58 $container->get('current_route_match')
65 protected function defineOptions() {
66 $options = parent::defineOptions();
67 $options['user'] = ['default' => ''];
75 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
77 '#type' => 'checkbox',
78 '#title' => $this->t('Also look for a node and use the node author'),
79 '#default_value' => $this->options['user'],
86 public function getArgument() {
88 // If there is a user object in the current route.
89 if ($user = $this->routeMatch->getParameter('user')) {
90 if ($user instanceof UserInterface) {
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();
106 public function getCacheMaxAge() {
107 return Cache::PERMANENT;
113 public function getCacheContexts() {