comparison core/lib/Drupal/Core/Condition/ConditionPluginBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Condition;
4
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;
10
11 /**
12 * Provides a basis for fulfilling contexts for condition plugins.
13 *
14 * @see \Drupal\Core\Condition\Annotation\Condition
15 * @see \Drupal\Core\Condition\ConditionInterface
16 * @see \Drupal\Core\Condition\ConditionManager
17 *
18 * @ingroup plugin_api
19 */
20 abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {
21
22 use ContextAwarePluginAssignmentTrait;
23
24 /**
25 * The condition manager to proxy execute calls through.
26 *
27 * @var \Drupal\Core\Executable\ExecutableInterface
28 */
29 protected $executableManager;
30
31 /**
32 * {@inheritdoc}
33 */
34 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
35 parent::__construct($configuration, $plugin_id, $plugin_definition);
36
37 $this->setConfiguration($configuration);
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function isNegated() {
44 return !empty($this->configuration['negate']);
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
51 if ($form_state instanceof SubformStateInterface) {
52 $form_state = $form_state->getCompleteFormState();
53 }
54 $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
55 $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
56 $form['negate'] = [
57 '#type' => 'checkbox',
58 '#title' => $this->t('Negate the condition'),
59 '#default_value' => $this->configuration['negate'],
60 ];
61 return $form;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
68 }
69
70 /**
71 * {@inheritdoc}
72 */
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'));
77 }
78 }
79
80 /**
81 * {@inheritdoc}
82 */
83 public function execute() {
84 return $this->executableManager->execute($this);
85 }
86
87 /**
88 * {@inheritdoc}
89 */
90 public function getConfiguration() {
91 return [
92 'id' => $this->getPluginId(),
93 ] + $this->configuration;
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function setConfiguration(array $configuration) {
100 $this->configuration = $configuration + $this->defaultConfiguration();
101 return $this;
102 }
103
104 /**
105 * {@inheritdoc}
106 */
107 public function defaultConfiguration() {
108 return [
109 'negate' => FALSE,
110 ];
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function calculateDependencies() {
117 return [];
118 }
119
120 /**
121 * {@inheritdoc}
122 */
123 public function setExecutableManager(ExecutableManagerInterface $executableManager) {
124 $this->executableManager = $executableManager;
125 return $this;
126 }
127
128 }