Domain.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = FALSE)
  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. $form = parent::buildConfigurationForm($form, $form_state);
  70. if (isset($form['context_mapping']['entity:domain']['#title'])) {
  71. $form['context_mapping']['entity:domain']['#title'] = $this->t('Select the Domain condition');
  72. $form['context_mapping']['entity:domain']['#description'] = $this->t('This value must be set to "Active domain" for the context to work.');
  73. }
  74. return $form;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function defaultConfiguration() {
  80. return [
  81. 'domains' => [],
  82. ] + parent::defaultConfiguration();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  88. $this->configuration['domains'] = array_filter($form_state->getValue('domains'));
  89. parent::submitConfigurationForm($form, $form_state);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function summary() {
  95. // Use the domain labels. They will be sanitized below.
  96. $domains = array_intersect_key(\Drupal::entityTypeManager()->getStorage('domain')->loadOptionsList(), $this->configuration['domains']);
  97. if (count($domains) > 1) {
  98. $domains = implode(', ', $domains);
  99. }
  100. else {
  101. $domains = reset($domains);
  102. }
  103. if ($this->isNegated()) {
  104. return $this->t('Active domain is not @domains', ['@domains' => $domains]);
  105. }
  106. else {
  107. return $this->t('Active domain is @domains', ['@domains' => $domains]);
  108. }
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function evaluate() {
  114. $domains = $this->configuration['domains'];
  115. if (empty($domains) && !$this->isNegated()) {
  116. return TRUE;
  117. }
  118. // If the context did not load, derive from the request.
  119. if (!$domain = $this->getContextValue('entity:domain')) {
  120. $domain = $this->domainNegotiator->getActiveDomain();
  121. }
  122. // No context found?
  123. if (empty($domain)) {
  124. return FALSE;
  125. }
  126. // NOTE: The context system handles negation for us.
  127. return (bool) in_array($domain->id(), $domains);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getCacheContexts() {
  133. $contexts = parent::getCacheContexts();
  134. $contexts[] = 'url.site';
  135. return $contexts;
  136. }
  137. }