ConditionPluginCollection.php 2.2 KB

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