Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Condition;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Context\ContextInterface;
|
Chris@0
|
6 use Drupal\Core\Plugin\DefaultLazyPluginCollection;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a collection of condition plugins.
|
Chris@0
|
10 */
|
Chris@0
|
11 class ConditionPluginCollection extends DefaultLazyPluginCollection {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * An array of collected contexts for conditions.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $conditionContexts = [];
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * {@inheritdoc}
|
Chris@0
|
22 *
|
Chris@0
|
23 * @return \Drupal\Core\Condition\ConditionInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 public function &get($instance_id) {
|
Chris@0
|
26 return parent::get($instance_id);
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * {@inheritdoc}
|
Chris@0
|
31 */
|
Chris@0
|
32 public function getConfiguration() {
|
Chris@0
|
33 $configuration = parent::getConfiguration();
|
Chris@0
|
34 // Remove configuration if it matches the defaults.
|
Chris@0
|
35 foreach ($configuration as $instance_id => $instance_config) {
|
Chris@0
|
36 $default_config = [];
|
Chris@0
|
37 $default_config['id'] = $instance_id;
|
Chris@0
|
38 $default_config += $this->get($instance_id)->defaultConfiguration();
|
Chris@0
|
39 // In order to determine if a plugin is configured, we must compare it to
|
Chris@0
|
40 // its default configuration. The default configuration of a plugin does
|
Chris@0
|
41 // not contain context_mapping and it is not used when the plugin is not
|
Chris@0
|
42 // configured, so remove the context_mapping from the instance config to
|
Chris@0
|
43 // compare the remaining values.
|
Chris@0
|
44 unset($instance_config['context_mapping']);
|
Chris@0
|
45 if ($default_config === $instance_config) {
|
Chris@0
|
46 unset($configuration[$instance_id]);
|
Chris@0
|
47 }
|
Chris@0
|
48 }
|
Chris@0
|
49 return $configuration;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Sets the condition context for a given name.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param string $name
|
Chris@0
|
56 * The name of the context.
|
Chris@0
|
57 * @param \Drupal\Component\Plugin\Context\ContextInterface $context
|
Chris@0
|
58 * The context to add.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return $this
|
Chris@0
|
61 */
|
Chris@0
|
62 public function addContext($name, ContextInterface $context) {
|
Chris@0
|
63 $this->conditionContexts[$name] = $context;
|
Chris@0
|
64 return $this;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Gets the values for all defined contexts.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
|
Chris@0
|
71 * An array of set contexts, keyed by context name.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function getConditionContexts() {
|
Chris@0
|
74 return $this->conditionContexts;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 }
|