TokensFilterTrait.php in Tokens in Views Filter Criteria 8
File
src/TokensFilterTrait.php
View source
<?php
namespace Drupal\token_views_filter;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
trait TokensFilterTrait {
protected $token;
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;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['use_tokens'] = [
'default' => FALSE,
];
return $options;
}
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',
],
];
}
}
public function preQuery() {
parent::preQuery();
if (!empty($this->options['use_tokens'])) {
$this
->replaceTokens($this->value);
}
}
}