You are here

public function DomainToken::getTokens in Domain Access 8

Implements hook_tokens().

File

domain/src/DomainToken.php, line 124

Class

DomainToken
Token handler for Domain.

Namespace

Drupal\domain

Code

public function getTokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  $domain = NULL;

  // Based on the type, get the proper domain context.
  switch ($type) {
    case 'domain':
      if (!empty($data['domain'])) {
        $domain = $data['domain'];
      }
      else {
        $domain = $this->negotiator
          ->getActiveDomain();
      }
      break;
    case 'current-domain':
      $domain = $this->negotiator
        ->getActiveDomain();
      break;
    case 'default-domain':
      $domain = $this->domainStorage
        ->loadDefaultDomain();
      break;
  }

  // Set the token information.
  if (!empty($domain)) {
    $callbacks = $this
      ->getCallbacks();
    foreach ($tokens as $name => $original) {
      if (isset($callbacks[$name])) {
        $replacements[$original] = $domain
          ->{$callbacks[$name]}();
        $bubbleable_metadata
          ->addCacheableDependency($domain);
      }
    }
  }
  return $replacements;
}