User.php in Zircon Profile 8.0
File
core/modules/user/src/Plugin/views/argument_default/User.php
View source
<?php
namespace Drupal\user\Plugin\views\argument_default;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\user\UserInterface;
use Drupal\node\NodeInterface;
class User extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
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'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['user'] = array(
'default' => '',
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['user'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Also look for a node and use the node author'),
'#default_value' => $this->options['user'],
);
}
public function getArgument() {
if ($user = $this->routeMatch
->getParameter('user')) {
if ($user instanceof UserInterface) {
return $user
->id();
}
}
if (!empty($this->options['user']) && ($node = $this->routeMatch
->getParameter('node'))) {
if ($node instanceof NodeInterface) {
return $node
->getOwnerId();
}
}
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'url',
];
}
}
Classes
Name |
Description |
User |
Default argument plugin to extract a user from request. |