ConditionPluginBase.php 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Condition\ConditionPluginBase.
 */

namespace Drupal\Core\Condition;

10 11
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
12
use Drupal\Core\Executable\ExecutableManagerInterface;
13
use Drupal\Core\Executable\ExecutablePluginBase;
14
use Drupal\Core\Form\FormStateInterface;
15
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
16 17 18

/**
 * Provides a basis for fulfilling contexts for condition plugins.
19 20 21 22 23 24
 *
 * @see \Drupal\Core\Condition\Annotation\Condition
 * @see \Drupal\Core\Condition\ConditionInterface
 * @see \Drupal\Core\Condition\ConditionManager
 *
 * @ingroup plugin_api
25 26 27
 */
abstract class ConditionPluginBase extends ExecutablePluginBase implements ConditionInterface {

28 29
  use ContextAwarePluginAssignmentTrait;

30 31 32 33 34 35 36
  /**
   * The condition manager to proxy execute calls through.
   *
   * @var \Drupal\Core\Executable\ExecutableInterface
   */
  protected $executableManager;

37
  /**
38
   * {@inheritdoc}
39
   */
40 41 42 43
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->setConfiguration($configuration);
44 45 46
  }

  /**
47
   * {@inheritdoc}
48 49 50 51 52 53
   */
  public function isNegated() {
    return !empty($this->configuration['negate']);
  }

  /**
54
   * {@inheritdoc}
55
   */
56
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
57
    $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
58
    $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
59 60
    $form['negate'] = array(
      '#type' => 'checkbox',
61
      '#title' => $this->t('Negate the condition'),
62
      '#default_value' => $this->configuration['negate'],
63 64 65 66 67
    );
    return $form;
  }

  /**
68
   * {@inheritdoc}
69
   */
70
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
71
  }
72 73

  /**
74
   * {@inheritdoc}
75
   */
76
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
77
    $this->configuration['negate'] = $form_state->getValue('negate');
78 79 80
  }

  /**
81
   * {@inheritdoc}
82 83 84 85 86
   */
  public function execute() {
    return $this->executableManager->execute($this);
  }

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return array(
      'id' => $this->getPluginId(),
    ) + $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration + $this->defaultConfiguration();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
108 109 110
    return array(
      'negate' => FALSE,
    );
111 112 113 114 115 116 117 118 119
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return array();
  }

120 121 122 123 124 125 126 127
  /**
   * {@inheritdoc}
   */
  public function setExecutableManager(ExecutableManagerInterface $executableManager) {
    $this->executableManager = $executableManager;
    return $this;
  }

128
}