View source
<?php
namespace Drupal\context\Plugin\Condition;
use Drupal\context\ContextManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContextAny extends ConditionPluginBase implements ContainerFactoryPluginInterface {
private $contextManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ContextManager $context_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contextManager = $context_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.manager'));
}
public function defaultConfiguration() {
return [
'values' => '',
] + parent::defaultConfiguration();
}
public function getCacheContexts() {
$cache_contexts = parent::getCacheContexts();
$contexts = array_filter(array_map('trim', explode("\n", $this->configuration['values'])));
foreach ($contexts as $id) {
$id = ltrim($id, '~');
$context = $this->contextManager
->getContext($id);
$context_conditions = $context
->getConditions();
foreach ($context_conditions as $condition) {
$cache_contexts = Cache::mergeContexts($cache_contexts, $condition
->getCacheContexts());
}
}
return $cache_contexts;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
unset($form['negate']);
$form['values'] = [
'#type' => 'textarea',
'#title' => $this
->t('Context (any)'),
'#description' => $this
->t('Set this context on the basis of other active contexts. Put each context on a separate line. The condition will pass if <em>any</em> of the contexts are active. You can use the <code>*</code> character (asterisk) as a wildcard and the <code>~</code> character (tilde) to prevent this context from activating if the listed context is active. Other contexts which use context conditions can not be used to exclude this context from activating.'),
'#default_value' => $this->configuration['values'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['values'] = $form_state
->getValue('values');
parent::submitConfigurationForm($form, $form_state);
}
public function summary() {
$contexts = array_map('trim', explode("\n", $this->configuration['values']));
$contexts = implode(', ', $contexts);
return $this
->t('Return true on the basis of other active contexts: @contexts', [
'@contexts' => $contexts,
]);
}
public function evaluate() {
$required_contexts = $negated_contexts = [];
$asterisk_context = '';
$values = array_filter(array_map('trim', explode("\n", $this->configuration['values'])));
if (empty($values)) {
return TRUE;
}
foreach ($values as $key) {
if (substr($key, 0, 1) == "~") {
$negated_contexts[] = substr($key, 1);
}
elseif (strpos($key, '*') !== FALSE) {
$asterisk_context = $key;
}
elseif (!empty($key)) {
$required_contexts[] = $key;
}
}
foreach ($negated_contexts as $name) {
$negated_context = $this->contextManager
->getContext($name);
if ($this->contextManager
->evaluateContextConditions($negated_context) && !$negated_context
->disabled()) {
return FALSE;
}
}
foreach ($required_contexts as $name) {
if ($required_context = $this->contextManager
->getContext($name)) {
if ($this->contextManager
->evaluateContextConditions($required_context) && !$required_context
->disabled()) {
return TRUE;
}
}
}
if ($asterisk_contexts = $this->contextManager
->getContext($asterisk_context)) {
foreach ($asterisk_contexts as $context) {
if ($this->contextManager
->evaluateContextConditions($context) && !$context
->disabled()) {
return TRUE;
}
}
}
return FALSE;
}
}