You are here

trait TokensFilterTrait in Tokens in Views Filter Criteria 8

Filter trait to use for filter plugins.

Hierarchy

4 files declare their use of TokensFilterTrait
TokensDateFilter.php in src/Plugin/views/filter/token/TokensDateFilter.php
TokensDateTimeFilter.php in src/Plugin/views/filter/token/TokensDateTimeFilter.php
TokensNumericFilter.php in src/Plugin/views/filter/token/TokensNumericFilter.php
TokensStringFilter.php in src/Plugin/views/filter/token/TokensStringFilter.php

File

src/TokensFilterTrait.php, line 11

Namespace

Drupal\token_views_filter
View source
trait TokensFilterTrait {

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->token = $container
      ->get('token');
    return $instance;
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\views\Plugin\views/PluginBase::defineOptions()
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['use_tokens'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * Provide a simple textfield options to use tokens in filter.
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['use_tokens'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use tokens'),
      '#default_value' => $this->options['use_tokens'],
    ];
    if (\Drupal::moduleHandler()
      ->moduleExists('token')) {
      $form['token_help'] = [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            ':input[name="options\\[use_tokens\\]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['token_help']['browser'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          'view',
          'current-page',
        ],
      ];
    }
  }

  /**
   * {@inheritdoc}
   *
   * Replace tokens
   */
  public function preQuery() {
    parent::preQuery();
    if (!empty($this->options['use_tokens'])) {
      $this
        ->replaceTokens($this->value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TokensFilterTrait::$token protected property The token service.
TokensFilterTrait::buildOptionsForm public function Provide a simple textfield options to use tokens in filter.
TokensFilterTrait::create public static function
TokensFilterTrait::defineOptions protected function
TokensFilterTrait::preQuery public function Replace tokens