View source
<?php
namespace Drupal\views_extras\Plugin\views\argument_default;
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;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
class TempStore extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected $currentUser;
protected $moduleHandler;
protected $token;
protected $tempStoreFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $currentUser, ModuleHandlerInterface $moduleHandler, Token $token, PrivateTempStoreFactory $temp_store_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $currentUser;
$this->moduleHandler = $moduleHandler;
$this->token = $token;
$this->tempStoreFactory = $temp_store_factory;
}
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'), $container
->get('tempstore.private'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['tempStore_unique_name'] = [
'default' => '',
];
$options['tempStore_key'] = [
'default' => '',
];
$options['fallback_value'] = [
'default' => FALSE,
];
$options['cache_time'] = [
'default' => -1,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['tempStore_unique_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Unique Name'),
'#default_value' => $this->options['tempStore_unique_name'],
'#description' => $this
->t('Unique namespace, used while setting the tempStore'),
];
$form['tempStore_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('TempStore key'),
'#default_value' => $this->options['tempStore_key'],
'#description' => $this
->t('Keys of tempStore variable'),
];
$form['fallback_value'] = [
'#type' => 'textfield',
'#title' => $this
->t('If tempStore 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'] = [
'#type' => 'markup',
'#token_types' => [
'user',
],
'#theme' => 'token_tree_link',
];
}
$form['cache_time'] = [
'#type' => 'number',
'#title' => $this
->t('Cache Maximum Age.'),
'#default_value' => $this->options['cache_time'],
'#description' => $this
->t('If tempStore value changes set it to 0.'),
];
}
public function getArgument() {
if (!empty($key = $this->options['tempStore_key'])) {
return $this->tempStoreFactory
->get($this->options['tempStore_unique_name'])
->get($this->options['tempStore_key']);
}
elseif (!empty($value = $this->options['fallback_value'])) {
return $this->token
->replace($value, [
'user' => $this->currentUser,
]);
}
}
public function getCacheMaxAge() {
return (int) $this->options['cache_time'];
}
public function getCacheContexts() {
return [
'session',
];
}
}