3 namespace Drupal\Core\Condition;
5 use Drupal\Core\Executable\ExecutableManagerInterface;
6 use Drupal\Core\Executable\ExecutablePluginBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Form\SubformStateInterface;
9 use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
12 * Provides a basis for fulfilling contexts for condition plugins.
14 * @see \Drupal\Core\Condition\Annotation\Condition
15 * @see \Drupal\Core\Condition\ConditionInterface
16 * @see \Drupal\Core\Condition\ConditionManager
20 abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {
22 use ContextAwarePluginAssignmentTrait;
25 * The condition manager to proxy execute calls through.
27 * @var \Drupal\Core\Executable\ExecutableInterface
29 protected $executableManager;
34 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
35 parent::__construct($configuration, $plugin_id, $plugin_definition);
37 $this->setConfiguration($configuration);
43 public function isNegated() {
44 return !empty($this->configuration['negate']);
50 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
51 if ($form_state instanceof SubformStateInterface) {
52 $form_state = $form_state->getCompleteFormState();
54 $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
55 $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
57 '#type' => 'checkbox',
58 '#title' => $this->t('Negate the condition'),
59 '#default_value' => $this->configuration['negate'],
67 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
73 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
74 $this->configuration['negate'] = $form_state->getValue('negate');
75 if ($form_state->hasValue('context_mapping')) {
76 $this->setContextMapping($form_state->getValue('context_mapping'));
83 public function execute() {
84 return $this->executableManager->execute($this);
90 public function getConfiguration() {
92 'id' => $this->getPluginId(),
93 ] + $this->configuration;
99 public function setConfiguration(array $configuration) {
100 $this->configuration = $configuration + $this->defaultConfiguration();
107 public function defaultConfiguration() {
116 public function calculateDependencies() {
123 public function setExecutableManager(ExecutableManagerInterface $executableManager) {
124 $this->executableManager = $executableManager;