1. 8.3.x core/lib/Drupal/Core/Condition/ConditionPluginBase.php
  2. 8.0.x core/lib/Drupal/Core/Condition/ConditionPluginBase.php
  3. 8.1.x core/lib/Drupal/Core/Condition/ConditionPluginBase.php
  4. 8.2.x core/lib/Drupal/Core/Condition/ConditionPluginBase.php
  5. 8.4.x core/lib/Drupal/Core/Condition/ConditionPluginBase.php

Namespace

Drupal\Core\Condition

File

core/lib/Drupal/Core/Condition/ConditionPluginBase.php
View source
  1. <?php
  2. namespace Drupal\Core\Condition;
  3. use Drupal\Core\Executable\ExecutableManagerInterface;
  4. use Drupal\Core\Executable\ExecutablePluginBase;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Form\SubformStateInterface;
  7. use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
  8. /**
  9. * Provides a basis for fulfilling contexts for condition plugins.
  10. *
  11. * @see \Drupal\Core\Condition\Annotation\Condition
  12. * @see \Drupal\Core\Condition\ConditionInterface
  13. * @see \Drupal\Core\Condition\ConditionManager
  14. *
  15. * @ingroup plugin_api
  16. */
  17. abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {
  18. use ContextAwarePluginAssignmentTrait;
  19. /**
  20. * The condition manager to proxy execute calls through.
  21. *
  22. * @var \Drupal\Core\Executable\ExecutableInterface
  23. */
  24. protected $executableManager;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  29. parent::__construct($configuration, $plugin_id, $plugin_definition);
  30. $this->setConfiguration($configuration);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function isNegated() {
  36. return !empty($this->configuration['negate']);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  42. if ($form_state instanceof SubformStateInterface) {
  43. $form_state = $form_state->getCompleteFormState();
  44. }
  45. $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
  46. $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
  47. $form['negate'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => $this->t('Negate the condition'),
  50. '#default_value' => $this->configuration['negate'],
  51. );
  52. return $form;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  63. $this->configuration['negate'] = $form_state->getValue('negate');
  64. if ($form_state->hasValue('context_mapping')) {
  65. $this->setContextMapping($form_state->getValue('context_mapping'));
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function execute() {
  72. return $this->executableManager->execute($this);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getConfiguration() {
  78. return array(
  79. 'id' => $this->getPluginId(),
  80. ) + $this->configuration;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setConfiguration(array $configuration) {
  86. $this->configuration = $configuration + $this->defaultConfiguration();
  87. return $this;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function defaultConfiguration() {
  93. return array(
  94. 'negate' => FALSE,
  95. );
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function calculateDependencies() {
  101. return array();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setExecutableManager(ExecutableManagerInterface $executableManager) {
  107. $this->executableManager = $executableManager;
  108. return $this;
  109. }
  110. }

Classes

Namesort descending Description
ConditionPluginBase Provides a basis for fulfilling contexts for condition plugins.