domain.api.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @file
  4. * API documentation file for Domain module.
  5. */
  6. /**
  7. * Notifies other modules that we are loading a domain record from the database.
  8. *
  9. * When using this hook, you should invoke the namespace with:
  10. *
  11. * use Drupal\domain\DomainInterface;
  12. *
  13. * @param \Drupal\domain\DomainInterface[] $domains
  14. * An array of $domain record objects.
  15. */
  16. function hook_domain_load(array $domains) {
  17. // Add a variable to the $domain.
  18. foreach ($domains as $domain) {
  19. $domain->addProperty('myvar', 'mydomainvar');
  20. }
  21. }
  22. /**
  23. * Allows modules to modify the inbound domain request.
  24. *
  25. * When using this hook, first check $domain->getMatchType(), which returns a
  26. * numeric constant indicating the type of match derived by the caller or by
  27. * earlier returns of this hook (such as domain_alias_request_alter()).
  28. * Use this value to determine if the request needs to be overridden. Valid
  29. * types are DomainNegotiator::DOMAIN_MATCH_NONE,
  30. * DomainNegotiator::DOMAIN_MATCH_EXACT, DomainNegotiator::DOMAIN_MATCH_ALIAS.
  31. *
  32. * To issue a redirect, as in the case of Domain Alias, set a redirect
  33. * property to a valid response code (301 or 302).
  34. *
  35. * @param \Drupal\domain\DomainInterface $domain
  36. * A domain object defined by Drupal\domain\DomainInterface.
  37. */
  38. function hook_domain_request_alter(\Drupal\domain\DomainInterface &$domain) {
  39. // Add a special case to the example domain.
  40. if ($domain->getMatchType() == \Drupal\domain\DomainNegotiator::DOMAIN_MATCH_EXACT && $domain->id() == 'example_com') {
  41. // Do something here.
  42. $domain->addProperty('foo', 'Bar');
  43. }
  44. }
  45. /**
  46. * Adds administrative operations for the domain overview form.
  47. *
  48. * These operations are only available to users who can administer the domain.
  49. * That access check happens prior to this hook being called. If your use-case
  50. * requires additional permission checking, you should provide it before
  51. * returning any values.
  52. *
  53. * @param \Drupal\domain\DomainInterface $domain
  54. * A domain record object.
  55. * @param \Drupal\Core\Session\AccountInterface $account
  56. * The user account performing the operation.
  57. *
  58. * @return array
  59. * An array of operations which uses a unique string key and requires the
  60. * elements 'title' and 'url'; the 'query' value is optional, and used
  61. * for link-actions with tokens
  62. */
  63. function hook_domain_operations(\Drupal\domain\DomainInterface $domain, \Drupal\Core\Session\AccountInterface $account) {
  64. $operations = [];
  65. // Check permissions.
  66. if ($account->hasPermission('view domain aliases') || $account->hasPermission('administer domain aliases')) {
  67. // Add aliases to the list.
  68. $id = $domain->id();
  69. $operations['domain_alias'] = [
  70. 'title' => t('Aliases'),
  71. 'url' => \Drupal\Core\Url::fromRoute('domain_alias.admin', ['domain' => $id]),
  72. 'weight' => 60,
  73. ];
  74. }
  75. return $operations;
  76. }
  77. /**
  78. * Alter the validation step of a domain record.
  79. *
  80. * This hook allows modules to change or extend how domain validation
  81. * happens. Most useful for international domains or other special cases
  82. * where a site wants to restrict domain creation is some manner.
  83. *
  84. * NOTE: This does not apply to Domain Alias records.
  85. *
  86. * No return value. Modify $error_list by reference. Return an empty array
  87. * or NULL to validate this domain.
  88. *
  89. * @param array &$error_list
  90. * The list of current validation errors. Modify this value by reference.
  91. * If you return an empty array or NULL, the domain is considered valid.
  92. * @param string $hostname
  93. * The HTTP_HOST string value being validated, such as one.example.com.
  94. * Note that this is checked for uniqueness separately. This value is not
  95. * modifiable.
  96. *
  97. * @see domain_valid_domain()
  98. */
  99. function hook_domain_validate_alter(array &$error_list, $hostname) {
  100. // Only allow TLDs to be .org for our site.
  101. if (substr($hostname, -4) != '.org') {
  102. $error_list[] = t('Only .org domains may be registered.');
  103. }
  104. }
  105. /**
  106. * Alter the list of domains that may be referenced.
  107. *
  108. * Note that this hook does not fire for users with the 'administer domains'
  109. * permission.
  110. *
  111. * No return value. Modify the $query object via methods.
  112. *
  113. * @param \Drupal\Core\Entity\Query\QueryInterface $query
  114. * An entity query prepared by DomainSelection::buildEntityQuery().
  115. * @param \Drupal\Core\Session\AccountInterface $account
  116. * The account of the user viewing the reference list.
  117. * @param array $context
  118. * A keyed array passing two items:
  119. * - entity_type The type of entity (e.g. node, user) that requested the list.
  120. * - bundle The entity subtype (e.g. 'article' or 'page').
  121. * - field_type The access field group used for this selection. Groups are
  122. * 'editor' for assigning editorial permissions (as in Domain Access)
  123. * 'admin' for assigning administrative permissions for a specific domain.
  124. * Most contributed modules will use 'editor'.
  125. */
  126. function hook_domain_references_alter(\Drupal\Core\Entity\Query\QueryInterface $query, \Drupal\Core\Session\AccountInterface $account, array $context) {
  127. // Remove the default domain from non-admins when editing nodes.
  128. if ($context['entity_type'] == 'node' && $context['field_type'] == 'editor' && !$account->hasPermission('edit assigned domains')) {
  129. $default = \Drupal::entityTypeManager()->getStorage('domain')->loadDefaultId();
  130. $query->condition('id', $default, '<>');
  131. }
  132. }