3 namespace Drupal\views\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\Path\AliasManagerInterface;
9 use Drupal\Core\Path\CurrentPathStack;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Default argument plugin to use the raw value from the URL.
15 * @ingroup views_argument_default_plugins
17 * @ViewsArgumentDefault(
19 * title = @Translation("Raw value from URL")
22 class Raw extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
27 * @var \Drupal\Core\Path\AliasManagerInterface
29 protected $aliasManager;
34 * @var \Drupal\Core\Path\CurrentPathStack
36 protected $currentPath;
39 * Constructs a Raw object.
41 * @param array $configuration
42 * A configuration array containing information about the plugin instance.
43 * @param string $plugin_id
44 * The plugin_id for the plugin instance.
45 * @param mixed $plugin_definition
46 * The plugin implementation definition.
47 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
49 * @param \Drupal\Core\Path\CurrentPathStack $current_path
52 public function __construct(array $configuration, $plugin_id, $plugin_definition, AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
53 parent::__construct($configuration, $plugin_id, $plugin_definition);
55 $this->aliasManager = $alias_manager;
56 $this->currentPath = $current_path;
62 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
67 $container->get('path.alias_manager'),
68 $container->get('path.current')
75 protected function defineOptions() {
76 $options = parent::defineOptions();
77 $options['index'] = ['default' => ''];
78 $options['use_alias'] = ['default' => FALSE];
86 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
87 parent::buildOptionsForm($form, $form_state);
90 '#title' => $this->t('Path component'),
91 '#default_value' => $this->options['index'],
92 // range(1, 10) returns an array with:
93 // - keys that count from 0 to match PHP array keys from explode().
94 // - values that count from 1 for display to humans.
95 '#options' => range(1, 10),
96 '#description' => $this->t('The numbering starts from 1, e.g. on the page admin/structure/types, the 3rd path component is "types".'),
98 $form['use_alias'] = [
99 '#type' => 'checkbox',
100 '#title' => $this->t('Use path alias'),
101 '#default_value' => $this->options['use_alias'],
102 '#description' => $this->t('Use path alias instead of internal path.'),
109 public function getArgument() {
110 // Don't trim the leading slash since getAliasByPath() requires it.
111 $path = rtrim($this->currentPath->getPath($this->view->getRequest()), '/');
112 if ($this->options['use_alias']) {
113 $path = $this->aliasManager->getAliasByPath($path);
115 $args = explode('/', $path);
116 // Drop the empty first element created by the leading slash since the path
117 // component index doesn't take it into account.
119 if (isset($args[$this->options['index']])) {
120 return $args[$this->options['index']];
127 public function getCacheMaxAge() {
128 return Cache::PERMANENT;
134 public function getCacheContexts() {