Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / views / argument-default.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\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
12 /**
13  * Example of argument default plugin.
14  *
15  * @ViewsArgumentDefault(
16  *   id = "{{ plugin_id }}",
17  *   title = @Translation("{{ plugin_label }}")
18  * )
19  */
20 class {{ class }} extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
21
22   /**
23    * The route match.
24    *
25    * @var \Drupal\Core\Routing\RouteMatchInterface
26    */
27   protected $routeMatch;
28
29   /**
30    * Constructs a new {{ class }} instance.
31    *
32    * @param array $configuration
33    *   A configuration array containing information about the plugin instance.
34    * @param string $plugin_id
35    *   The plugin_id for the plugin instance.
36    * @param mixed $plugin_definition
37    *   The plugin implementation definition.
38    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
39    *   The route match.
40    */
41   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
42     parent::__construct($configuration, $plugin_id, $plugin_definition);
43
44     // @DCG
45     // The Route match service is used to extract argument from the current
46     // route.
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['example_option'] = ['default' => ''];
68     return $options;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
75     $form['example_option'] = [
76       '#type' => 'textfield',
77       '#title' => t('Some example option'),
78       '#default_value' => $this->options['example_option'],
79     ];
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getArgument() {
86
87     // @DCG
88     // Here is the place where you should create a default argument for the
89     // contextual filter. The source of this argument depends on your needs.
90     // For example, you can extract the value from the URL or fetch it from
91     // some fields of the current viewed entity.
92     // For now let's use example option as an argument.
93     $argument = $this->options['example_option'];
94
95     return $argument;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getCacheMaxAge() {
102     return Cache::PERMANENT;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function getCacheContexts() {
109     return ['url'];
110   }
111
112 }