ConditionPluginBase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\ExecutableManagerInterface
  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'] = [
  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 [
  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 [
  94. 'negate' => FALSE,
  95. ];
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function calculateDependencies() {
  101. return [];
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setExecutableManager(ExecutableManagerInterface $executableManager) {
  107. $this->executableManager = $executableManager;
  108. return $this;
  109. }
  110. }