domain.module 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @file
  4. * Defines a Domain concept for use with Drupal.
  5. */
  6. use Drupal\Core\Render\BubbleableMetadata;
  7. use Drupal\Core\Url;
  8. use Drupal\domain\DomainInterface;
  9. use Drupal\Core\Routing\RouteMatchInterface;
  10. use Drupal\Component\Utility\Html;
  11. /**
  12. * The name of the node access control field.
  13. */
  14. const DOMAIN_ADMIN_FIELD = 'field_domain_admin';
  15. /**
  16. * Entity URI callback.
  17. *
  18. * @param \Drupal\domain\DomainInterface $domain
  19. * The Domain object.
  20. *
  21. * @return \Drupal\Core\Url
  22. * The Domain URL.
  23. */
  24. function domain_uri(DomainInterface $domain) {
  25. return Url::fromUri($domain->getPath(), ['absolute' => TRUE]);
  26. }
  27. /**
  28. * Implements hook_entity_load().
  29. *
  30. * The $domain->path and $domain->uri properties are derived from data in the
  31. * {domain} table. We use the hook system to load that data to indicate that
  32. * the data is not native to the object.
  33. *
  34. * This action is performed in hook_entity_load(), which precedes the running
  35. * of hook_domain_load() and ensures that our data is present for other modules.
  36. */
  37. function domain_entity_load(array $entities, $entity_type) {
  38. if ($entity_type == 'domain') {
  39. foreach ($entities as $domain) {
  40. $domain->setPath();
  41. $domain->setUrl();
  42. }
  43. }
  44. }
  45. /**
  46. * Implements hook_help().
  47. */
  48. function domain_help($route_name, RouteMatchInterface $route_match) {
  49. switch ($route_name) {
  50. case 'domain.admin':
  51. $output = t('<p>The following domains have been created for your site. The currently active domain
  52. <strong>is shown in boldface</strong>. You may click on a domain to change the currently active domain.
  53. </p>');
  54. return $output;
  55. }
  56. }
  57. /**
  58. * Implements hook_token_info().
  59. */
  60. function domain_token_info() {
  61. return \Drupal::service('domain.token')->getTokenInfo();
  62. }
  63. /**
  64. * Implements hook_tokens().
  65. */
  66. function domain_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  67. return \Drupal::service('domain.token')->getTokens($type, $tokens, $data, $options, $bubbleable_metadata);
  68. }
  69. /**
  70. * Implements hook_preprocess_HOOK() for html.html.twig.
  71. */
  72. function domain_preprocess_html(array &$variables) {
  73. // Add class to body tag, if set.
  74. $config = \Drupal::config('domain.settings');
  75. if ($string = $config->get('css_classes')) {
  76. $token = \Drupal::token();
  77. // Prepare the classes proparly, with one class per string.
  78. $classes = explode(' ', trim($string));
  79. foreach ($classes as $class) {
  80. // Ensure no leading or trailing space.
  81. $class = trim($class);
  82. if (!empty($class)) {
  83. $variables['attributes']['class'][] = Html::getClass($token->replace($class));
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\user\UserForm.
  90. *
  91. * Handle settings that the user cannot access.
  92. */
  93. function domain_form_user_form_alter(&$form, &$form_state, $form_id) {
  94. // Add the options hidden from the user silently to the form.
  95. $manager = \Drupal::service('domain.element_manager');
  96. $form = $manager->setFormOptions($form, $form_state, DOMAIN_ADMIN_FIELD);
  97. }
  98. /**
  99. * Implements hook_domain_references_alter().
  100. */
  101. function domain_domain_references_alter($query, $account, $context) {
  102. // Restrict domains by assignment, being sure only to act on the admin field.
  103. if ($context['field_type'] == 'admin' && $context['entity_type'] == 'user') {
  104. if ($account->hasPermission('administer domains')) {
  105. // Do nothing.
  106. }
  107. elseif ($account->hasPermission('assign domain administrators')) {
  108. $allowed = \Drupal::service('domain.element_manager')->getFieldValues($account, DOMAIN_ADMIN_FIELD);
  109. $query->condition('id', array_keys($allowed), 'IN');
  110. }
  111. else {
  112. // Remove all options.
  113. $query->condition('id', '-no-possible-match-');
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_views_data_alter().
  119. */
  120. function domain_views_data_alter(array &$data) {
  121. $table = 'user__' . DOMAIN_ADMIN_FIELD;
  122. // Since domains are not stored in the database, relationships cannot be used.
  123. unset($data[$table][DOMAIN_ADMIN_FIELD]['relationship']);
  124. }
  125. /**
  126. * Implements hook_theme().
  127. */
  128. function domain_theme() {
  129. return [
  130. 'domain_nav_block' => [
  131. 'render element' => 'items',
  132. ],
  133. ];
  134. }
  135. /**
  136. * Prepares variables for block templates.
  137. *
  138. * Default template: domain-nav-block.html.twig.
  139. *
  140. * @param array $variables
  141. * An associative array containing:
  142. * - items: An array of labels and urls for use in the list.
  143. * Properties used: 'label', 'url', 'active'.
  144. */
  145. function template_preprocess_domain_nav_block(array &$variables) {
  146. $variables['items'] = $variables['items']['#items'];
  147. }