ConditionPluginBase.php 2.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\Core\Condition\ConditionPluginBase.
 */

namespace Drupal\Core\Condition;

10
use Drupal\Core\Executable\ExecutableManagerInterface;
11
use Drupal\Core\Executable\ExecutablePluginBase;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
14
15
16

/**
 * Provides a basis for fulfilling contexts for condition plugins.
17
18
19
20
21
22
 *
 * @see \Drupal\Core\Condition\Annotation\Condition
 * @see \Drupal\Core\Condition\ConditionInterface
 * @see \Drupal\Core\Condition\ConditionManager
 *
 * @ingroup plugin_api
23
24
25
 */
abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {

26
27
  use ContextAwarePluginAssignmentTrait;

28
29
30
31
32
33
34
  /**
   * The condition manager to proxy execute calls through.
   *
   * @var \Drupal\Core\Executable\ExecutableInterface
   */
  protected $executableManager;

35
  /**
36
   * {@inheritdoc}
37
   */
38
39
40
41
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->setConfiguration($configuration);
42
43
44
  }

  /**
45
   * {@inheritdoc}
46
47
48
49
50
51
   */
  public function isNegated() {
    return !empty($this->configuration['negate']);
  }

  /**
52
   * {@inheritdoc}
53
   */
54
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
55
    $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
56
    $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
57
58
    $form['negate'] = array(
      '#type' => 'checkbox',
59
      '#title' => $this->t('Negate the condition'),
60
      '#default_value' => $this->configuration['negate'],
61
62
63
64
65
    );
    return $form;
  }

  /**
66
   * {@inheritdoc}
67
   */
68
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
69
  }
70
71

  /**
72
   * {@inheritdoc}
73
   */
74
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
75
    $this->configuration['negate'] = $form_state->getValue('negate');
76
77
78
  }

  /**
79
   * {@inheritdoc}
80
81
82
83
84
   */
  public function execute() {
    return $this->executableManager->execute($this);
  }

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return array(
      'id' => $this->getPluginId(),
    ) + $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration + $this->defaultConfiguration();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
106
107
108
    return array(
      'negate' => FALSE,
    );
109
110
111
112
113
114
115
116
117
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return array();
  }

118
119
120
121
122
123
124
125
  /**
   * {@inheritdoc}
   */
  public function setExecutableManager(ExecutableManagerInterface $executableManager) {
    $this->executableManager = $executableManager;
    return $this;
  }

126
}