3 namespace Drupal\Core\Condition;
5 use Drupal\Component\Plugin\Context\ContextInterface;
6 use Drupal\Core\Plugin\DefaultLazyPluginCollection;
9 * Provides a collection of condition plugins.
11 class ConditionPluginCollection extends DefaultLazyPluginCollection {
14 * An array of collected contexts for conditions.
16 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
18 protected $conditionContexts = [];
23 * @return \Drupal\Core\Condition\ConditionInterface
25 public function &get($instance_id) {
26 return parent::get($instance_id);
32 public function getConfiguration() {
33 $configuration = parent::getConfiguration();
34 // Remove configuration if it matches the defaults.
35 foreach ($configuration as $instance_id => $instance_config) {
37 $default_config['id'] = $instance_id;
38 $default_config += $this->get($instance_id)->defaultConfiguration();
39 // In order to determine if a plugin is configured, we must compare it to
40 // its default configuration. The default configuration of a plugin does
41 // not contain context_mapping and it is not used when the plugin is not
42 // configured, so remove the context_mapping from the instance config to
43 // compare the remaining values.
44 unset($instance_config['context_mapping']);
45 if ($default_config === $instance_config) {
46 unset($configuration[$instance_id]);
49 return $configuration;
53 * Sets the condition context for a given name.
56 * The name of the context.
57 * @param \Drupal\Component\Plugin\Context\ContextInterface $context
62 public function addContext($name, ContextInterface $context) {
63 $this->conditionContexts[$name] = $context;
68 * Gets the values for all defined contexts.
70 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
71 * An array of set contexts, keyed by context name.
73 public function getConditionContexts() {
74 return $this->conditionContexts;