You are here

public function DomainSourceToken::getTokens in Domain Access 8

Implements hook_tokens().

File

domain_source/src/DomainSourceToken.php, line 53

Class

DomainSourceToken
Token handler for Domain Source.

Namespace

Drupal\domain_source

Code

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

  // Based on the type, get the proper domain context.
  switch ($type) {
    case 'node':
      foreach ($tokens as $name => $original) {
        if ($name !== 'canonical-source-domain-url') {
          continue;
        }
        if (!empty($data['node'])) {

          /** @var \Drupal\node\NodeInterface $node */
          $node = $data['node'];
          $original = $tokens['canonical-source-domain-url'];
          if (in_array('canonical', $this
            ->getExcludedRoutes()) && $node
            ->hasField('field_domain_source') && !$node->field_domain_source
            ->isEmpty()) {

            /** @var \Drupal\domain\Domain $sourceDomain */
            $sourceDomain = $node->field_domain_source->entity;
            $url = $node
              ->toUrl('canonical')
              ->toString();
            $replacements[$original] = $sourceDomain
              ->buildUrl($url);
            $bubbleable_metadata
              ->addCacheableDependency($sourceDomain);
          }
          else {
            $replacements[$original] = $node
              ->toUrl('canonical')
              ->setAbsolute()
              ->toString();
          }
        }
      }
      break;
  }
  return $replacements;
}