View source
<?php
namespace Drupal\views_extras\Plugin\views\argument_default;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Utility\Token;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Cookie extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected $current_user;
protected $moduleHandler;
protected $token;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $current_user, ModuleHandlerInterface $moduleHandler, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->current_user = $current_user;
$this->moduleHandler = $moduleHandler;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('module_handler'), $container
->get('token'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['cookie_key'] = array(
'default' => '',
);
$options['fallback_value'] = array(
'default' => FALSE,
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['cookie_key'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Cookie variable key'),
'#default_value' => $this->options['cookie_key'],
'#description' => $this
->t('Key of Cookie variable, e.g. for $_COOKIE["key"], the key would be "key".'),
);
$form['fallback_value'] = array(
'#type' => 'textfield',
'#title' => $this
->t('If cookie variable is not set, what should be the fallback value.'),
'#default_value' => $this->options['fallback_value'],
'#description' => $this
->t('You may use user tokens.'),
);
if ($this->moduleHandler
->moduleExists("token")) {
$form['token_help'] = array(
'#type' => 'markup',
'#token_types' => array(
'user',
),
'#theme' => 'token_tree_link',
);
}
}
public function getArgument() {
if (!empty($key = $this->options['cookie_key']) && !empty($_COOKIE['Drupal_visitor_' . $key])) {
return $_COOKIE['Drupal_visitor_' . $key];
}
if (!empty($value = $this->options['fallback_value'])) {
return $this->token
->replace($value, [
'user' => $this->current_user,
]);
}
return $value;
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
$cookie = $this->options['cookie_key'];
$cookie = 'Drupal_visitor_' . $cookie;
return [
'cookies:' . $cookie,
];
}
}