You are here

class DomainSourceToken in Domain Access 8

Token handler for Domain Source.

TokenAPI still uses procedural code, but we have moved it to a class for easier refactoring.

Hierarchy

Expanded class hierarchy of DomainSourceToken

1 string reference to 'DomainSourceToken'
domain_source.services.yml in domain_source/domain_source.services.yml
domain_source/domain_source.services.yml
1 service uses DomainSourceToken
domain_source.token in domain_source/domain_source.services.yml
Drupal\domain_source\DomainSourceToken

File

domain_source/src/DomainSourceToken.php, line 15

Namespace

Drupal\domain_source
View source
class DomainSourceToken {
  use StringTranslationTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a DomainSourceToken object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Implements hook_token_info().
   */
  public function getTokenInfo() {

    // Domain Source tokens.
    $info['tokens']['node']['canonical-source-domain-url'] = [
      'name' => $this
        ->t('Canonical Source Domain URL'),
      'description' => $this
        ->t("The canonical URL from the source domain for this node."),
      'type' => 'node',
    ];
    return $info;
  }

  /**
   * Implements hook_tokens().
   */
  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;
  }

  /**
   * Gets the settings for domain source path rewrites.
   *
   * @return array
   *   The settings for domain source path rewrites.
   */
  public function getExcludedRoutes() {
    if (!isset($this->excludedRoutes)) {
      $config = $this->configFactory
        ->get('domain_source.settings');
      $routes = $config
        ->get('exclude_routes');
      if (is_array($routes)) {
        $this->excludedRoutes = array_flip($routes);
      }
      else {
        $this->excludedRoutes = [];
      }
    }
    return $this->excludedRoutes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainSourceToken::$configFactory protected property The config factory.
DomainSourceToken::getExcludedRoutes public function Gets the settings for domain source path rewrites.
DomainSourceToken::getTokenInfo public function Implements hook_token_info().
DomainSourceToken::getTokens public function Implements hook_tokens().
DomainSourceToken::__construct public function Constructs a DomainSourceToken object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.