View source
<?php
namespace Drupal\views_argument_token\Plugin\views\argument_default;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Drupal\Core\Url;
class TokenArgument extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected function defineOptions() {
$options = parent::defineOptions();
$options['argument'] = array(
'default' => '',
);
$options['process'] = array(
'default' => 0,
);
$options['and_or'] = array(
'default' => '+',
);
$options['all_option'] = array(
'default' => TRUE,
);
$options['debug'] = array(
'default' => 0,
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['argument'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Token'),
'#default_value' => $this->options['argument'],
);
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$form['token'] = \Drupal::service('views_argument_token.token')
->tokenBrowser();
}
$form['process'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Get fields raw values'),
'#description' => $this
->t('Get raw values of fields (only fields are supported).<br/>For example, get ID instead of title for entity reference fields.'),
'#default_value' => $this->options['process'],
);
$form['and_or'] = array(
'#type' => 'radios',
'#title' => $this
->t('Multiple values handling condition'),
'#options' => array(
'+' => $this
->t('Or'),
',' => $this
->t('And'),
),
'#default_value' => $this->options['and_or'],
'#states' => array(
'invisible' => array(
':input[name="options[argument_default][token][process]"]' => array(
'checked' => FALSE,
),
),
),
'#description' => $this
->t('You should authorize multiple values at the bottom of this form.'),
);
$form['all_option'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Send "all" if no value'),
'#default_value' => $this->options['all_option'],
'#description' => $this
->t('You should enable the "all" argument below.'),
);
$form['debug'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Show debug'),
'#default_value' => $this->options['debug'],
'#description' => $this
->t('Show as a message the argument value for debugging purposes.'),
);
}
public function getArgument() {
$token_service = \Drupal::token();
$argument = $this->options['argument'];
$process = $this->options['process'];
$debug = $this->options['debug'];
$all_option = $this->options['all_option'];
$tokens = $this
->tokenScan($argument);
if (isset($tokens['current-user'])) {
if ($process) {
$current_user = \Drupal::currentUser();
$account = user_load($current_user
->id());
$argument = $this
->processToken($account, 'current-user', $argument);
}
if (strpos($argument, 'current-user')) {
if (!isset($current_user)) {
$current_user = \Drupal::currentUser();
}
$argument = $token_service
->replace($argument, [
'current-user' => $current_user,
], [
'clear' => TRUE,
]);
}
}
$current_path = \Drupal::service('path.current')
->getPath();
$params = Url::fromUri("internal:" . $current_path)
->getRouteParameters();
$entity_type = key($params);
if (isset($tokens[$entity_type]) && ($entity = \Drupal::request()->attributes
->get($entity_type))) {
if ($process) {
$argument = $this
->processToken($entity, $entity_type, $argument);
}
if (strpos($argument, $entity_type)) {
$argument = $token_service
->replace($argument, [
$entity_type => $entity,
], [
'clear' => TRUE,
]);
}
}
$argument = PlainTextOutput::renderFromHtml($argument);
if ($debug) {
drupal_set_message($argument);
}
if (!$argument) {
if (!$all_option) {
return '';
}
$argument = 'all';
}
$argument = $this
->cleanArgumentValue($argument);
return $argument;
}
public function processToken(FieldableEntityInterface $entity, $entity_type, $argument) {
$and_or = $this->options['and_or'];
$field_names = $this
->tokenScan($argument);
foreach ($field_names[$entity_type] as $field_name => $token) {
$replace_values = [];
if ($entity
->hasField($field_name)) {
$field_values = $entity
->get($field_name)
->getValue();
foreach ($field_values as $field_value) {
$replace_values[] = array_values($field_value)[0];
}
$replace = implode($and_or, $replace_values);
$argument = str_replace($token, $replace, $argument);
}
}
return $argument;
}
public function tokenScan($text) {
preg_match_all('/
\\[ # [ - pattern start
([^\\s\\[\\]:]*) # match $type not containing whitespace : [ or ]
: # : - separator
([^\\[\\]]*) # match $name not containing [ or ]
\\] # ] - pattern end
/x', $text, $matches);
$types = $matches[1];
$tokens = $matches[2];
$results = array();
for ($i = 0; $i < count($tokens); $i++) {
$results[$types[$i]][$tokens[$i]] = $matches[0][$i];
}
return $results;
}
public function cleanArgumentValue($argument) {
if ($argument[0] == '+' || $argument[0] == ',') {
$argument = substr($argument, 1);
}
if ($argument[strlen($argument) - 1] == '+' || $argument[strlen($argument) - 1] == ',') {
$argument = substr($argument, 0, -1);
}
return $argument;
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [];
}
}