DomainToken.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Drupal\domain;
  3. use Drupal\Core\Entity\EntityTypeManagerInterface;
  4. use Drupal\Core\Render\BubbleableMetadata;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. /**
  7. * Token handler for Domain.
  8. *
  9. * TokenAPI still uses procedural code, but we have moved it to a class for
  10. * easier refactoring.
  11. */
  12. class DomainToken {
  13. use StringTranslationTrait;
  14. /**
  15. * The entity type manager.
  16. *
  17. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  18. */
  19. protected $entityTypeManager;
  20. /**
  21. * The Domain storage handler.
  22. *
  23. * @var \Drupal\domain\DomainStorageInterface
  24. */
  25. protected $domainStorage;
  26. /**
  27. * The Domain negotiator.
  28. *
  29. * @var \Drupal\domain\DomainNegotiatorInterface
  30. */
  31. protected $negotiator;
  32. /**
  33. * Constructs a DomainToken object.
  34. *
  35. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  36. * The entity type manager.
  37. * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
  38. * The domain negotiator.
  39. */
  40. public function __construct(EntityTypeManagerInterface $entity_type_manager, DomainNegotiatorInterface $negotiator) {
  41. $this->entityTypeManager = $entity_type_manager;
  42. $this->domainStorage = $this->entityTypeManager->getStorage('domain');
  43. $this->negotiator = $negotiator;
  44. }
  45. /**
  46. * Implements hook_token_info().
  47. */
  48. public function getTokenInfo() {
  49. // Domain token types.
  50. $info['types']['domain'] = [
  51. 'name' => $this->t('Domains'),
  52. 'description' => $this->t('Tokens related to domains.'),
  53. 'needs-data' => 'domain',
  54. ];
  55. // These two types require the Token contrib module.
  56. $info['types']['current-domain'] = [
  57. 'name' => $this->t('Current domain'),
  58. 'description' => $this->t('Tokens related to the current domain.'),
  59. 'type' => 'domain',
  60. ];
  61. $info['types']['default-domain'] = [
  62. 'name' => $this->t('Default domain'),
  63. 'description' => $this->t('Tokens related to the default domain.'),
  64. 'type' => 'domain',
  65. ];
  66. // Domain tokens.
  67. $info['tokens']['domain']['id'] = [
  68. 'name' => $this->t('Domain id'),
  69. 'description' => $this->t("The domain's numeric ID."),
  70. ];
  71. $info['tokens']['domain']['machine-name'] = [
  72. 'name' => $this->t('Domain machine name'),
  73. 'description' => $this->t('The domain machine identifier.'),
  74. ];
  75. $info['tokens']['domain']['path'] = [
  76. 'name' => $this->t('Domain path'),
  77. 'description' => $this->t('The base URL for the domain.'),
  78. ];
  79. $info['tokens']['domain']['name'] = [
  80. 'name' => $this->t('Domain name'),
  81. 'description' => $this->t('The domain name.'),
  82. ];
  83. $info['tokens']['domain']['url'] = [
  84. 'name' => $this->t('Domain URL'),
  85. 'description' => $this->t("The domain's URL for the current page request."),
  86. ];
  87. $info['tokens']['domain']['hostname'] = [
  88. 'name' => $this->t('Domain hostname'),
  89. 'description' => $this->t('The domain hostname.'),
  90. ];
  91. $info['tokens']['domain']['scheme'] = [
  92. 'name' => $this->t('Domain scheme'),
  93. 'description' => $this->t('The domain scheme.'),
  94. ];
  95. $info['tokens']['domain']['status'] = [
  96. 'name' => $this->t('Domain status'),
  97. 'description' => $this->t('The domain status.'),
  98. ];
  99. $info['tokens']['domain']['weight'] = [
  100. 'name' => $this->t('Domain weight'),
  101. 'description' => $this->t('The domain weight.'),
  102. ];
  103. $info['tokens']['domain']['is_default'] = [
  104. 'name' => $this->t('Domain default'),
  105. 'description' => $this->t('The domain is the default domain.'),
  106. ];
  107. return $info;
  108. }
  109. /**
  110. * Implements hook_tokens().
  111. */
  112. public function getTokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  113. $replacements = [];
  114. $domain = NULL;
  115. // Based on the type, get the proper domain context.
  116. switch ($type) {
  117. case 'domain':
  118. if (!empty($data['domain'])) {
  119. $domain = $data['domain'];
  120. }
  121. else {
  122. $domain = $this->negotiator->getActiveDomain();
  123. }
  124. break;
  125. case 'current-domain':
  126. $domain = $this->negotiator->getActiveDomain();
  127. break;
  128. case 'default-domain':
  129. $domain = $this->domainStorage->loadDefaultDomain();
  130. break;
  131. }
  132. // Set the token information.
  133. if (!empty($domain)) {
  134. $callbacks = $this->getCallbacks();
  135. foreach ($tokens as $name => $original) {
  136. if (isset($callbacks[$name])) {
  137. $replacements[$original] = $domain->{$callbacks[$name]}();
  138. $bubbleable_metadata->addCacheableDependency($domain);
  139. }
  140. }
  141. }
  142. return $replacements;
  143. }
  144. /**
  145. * Maps tokens to their entity callbacks.
  146. *
  147. * We assume that the token will call an instance of DomainInterface.
  148. *
  149. * @return array
  150. * An array of callbacks keyed by the token string.
  151. */
  152. public function getCallbacks() {
  153. return [
  154. 'id' => 'getDomainId',
  155. 'machine-name' => 'id',
  156. 'path' => 'getPath',
  157. 'name' => 'label',
  158. 'hostname' => 'getHostname',
  159. 'scheme' => 'getScheme',
  160. 'status' => 'status',
  161. 'weight' => 'getWeight',
  162. 'is_default' => 'isDefault',
  163. 'url' => 'getUrl',
  164. ];
  165. }
  166. }