Domain.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Drupal\domain\Plugin\Condition;
  3. use Drupal\Core\Condition\ConditionPluginBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\domain\DomainNegotiator;
  6. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9. * Provides a 'Domain' condition.
  10. *
  11. * @Condition(
  12. * id = "domain",
  13. * label = @Translation("Domain"),
  14. * context = {
  15. * "entity:domain" = @ContextDefinition("entity:domain", label = @Translation("Domain"), required = TRUE)
  16. * }
  17. * )
  18. */
  19. class Domain extends ConditionPluginBase implements ContainerFactoryPluginInterface {
  20. /**
  21. * The domain negotiator.
  22. *
  23. * @var \Drupal\domain\DomainNegotiator
  24. */
  25. protected $domainNegotiator;
  26. /**
  27. * Constructs a Domain condition plugin.
  28. *
  29. * @param \Drupal\domain\DomainNegotiator $domain_negotiator
  30. * The domain negotiator service.
  31. * @param array $configuration
  32. * A configuration array containing information about the plugin instance.
  33. * @param string $plugin_id
  34. * The plugin_id for the plugin instance.
  35. * @param mixed $plugin_definition
  36. * The plugin implementation definition.
  37. */
  38. public function __construct(DomainNegotiator $domain_negotiator, array $configuration, $plugin_id, $plugin_definition) {
  39. parent::__construct($configuration, $plugin_id, $plugin_definition);
  40. $this->domainNegotiator = $domain_negotiator;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  46. return new static(
  47. $container->get('domain.negotiator'),
  48. $configuration,
  49. $plugin_id,
  50. $plugin_definition
  51. );
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  57. $form['domains'] = [
  58. '#type' => 'checkboxes',
  59. '#title' => $this->t('When the following domains are active'),
  60. '#default_value' => $this->configuration['domains'],
  61. '#options' => array_map('\Drupal\Component\Utility\Html::escape', \Drupal::entityTypeManager()->getStorage('domain')->loadOptionsList()),
  62. '#description' => $this->t('If you select no domains, the condition will evaluate to TRUE for all requests.'),
  63. '#attached' => [
  64. 'library' => [
  65. 'domain/drupal.domain',
  66. ],
  67. ],
  68. ];
  69. return parent::buildConfigurationForm($form, $form_state);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function defaultConfiguration() {
  75. return [
  76. 'domains' => [],
  77. ] + parent::defaultConfiguration();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  83. $this->configuration['domains'] = array_filter($form_state->getValue('domains'));
  84. parent::submitConfigurationForm($form, $form_state);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function summary() {
  90. // Use the domain labels. They will be sanitized below.
  91. $domains = array_intersect_key(\Drupal::entityTypeManager()->getStorage('domain')->loadOptionsList(), $this->configuration['domains']);
  92. if (count($domains) > 1) {
  93. $domains = implode(', ', $domains);
  94. }
  95. else {
  96. $domains = reset($domains);
  97. }
  98. if ($this->isNegated()) {
  99. return $this->t('Active domain is not @domains', ['@domains' => $domains]);
  100. }
  101. else {
  102. return $this->t('Active domain is @domains', ['@domains' => $domains]);
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function evaluate() {
  109. $domains = $this->configuration['domains'];
  110. if (empty($domains) && !$this->isNegated()) {
  111. return TRUE;
  112. }
  113. // If the context did not load, derive from the request.
  114. if (!$domain = $this->getContextValue('entity:domain')) {
  115. $domain = $this->domainNegotiator->getActiveDomain();
  116. }
  117. // No context found?
  118. if (empty($domain)) {
  119. return FALSE;
  120. }
  121. // NOTE: The context system handles negation for us.
  122. return (bool) in_array($domain->id(), $domains);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getCacheContexts() {
  128. $contexts = parent::getCacheContexts();
  129. $contexts[] = 'url.site';
  130. return $contexts;
  131. }
  132. }