Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / views / argument-default.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/views/argument-default.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/views/argument-default.twig
new file mode 100644 (file)
index 0000000..c1c7ae5
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\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;
+
+/**
+ * Example of argument default plugin.
+ *
+ * @ViewsArgumentDefault(
+ *   id = "{{ plugin_id }}",
+ *   title = @Translation("{{ plugin_label }}")
+ * )
+ */
+class {{ class }} extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
+
+  /**
+   * The route match.
+   *
+   * @var \Drupal\Core\Routing\RouteMatchInterface
+   */
+  protected $routeMatch;
+
+  /**
+   * Constructs a new {{ class }} instance.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
+   *   The route match.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+
+    // @DCG
+    // The Route match service is used to extract argument from the current
+    // route.
+    $this->routeMatch = $route_match;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  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')
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function defineOptions() {
+    $options = parent::defineOptions();
+    $options['example_option'] = ['default' => ''];
+    return $options;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
+    $form['example_option'] = [
+      '#type' => 'textfield',
+      '#title' => t('Some example option'),
+      '#default_value' => $this->options['example_option'],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getArgument() {
+
+    // @DCG
+    // Here is the place where you should create a default argument for the
+    // contextual filter. The source of this argument depends on your needs.
+    // For example, you can extract the value from the URL or fetch it from
+    // some fields of the current viewed entity.
+    // For now let's use example option as an argument.
+    $argument = $this->options['example_option'];
+
+    return $argument;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheMaxAge() {
+    return Cache::PERMANENT;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheContexts() {
+    return ['url'];
+  }
+
+}