3 namespace Drupal\{{ machine_name }}\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;
13 * Example of argument default plugin.
15 * @ViewsArgumentDefault(
16 * id = "{{ plugin_id }}",
17 * title = @Translation("{{ plugin_label }}")
20 class {{ class }} extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
25 * @var \Drupal\Core\Routing\RouteMatchInterface
27 protected $routeMatch;
30 * Constructs a new {{ class }} instance.
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
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
45 // The Route match service is used to extract argument from the current
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['example_option'] = ['default' => ''];
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'],
85 public function getArgument() {
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'];
101 public function getCacheMaxAge() {
102 return Cache::PERMANENT;
108 public function getCacheContexts() {