You are here

function external_hreflang_tokens in External Hreflang 8

Implements hook_tokens().

File

./external_hreflang.tokens.inc, line 34
Token related functions for the external_hreflang.

Code

function external_hreflang_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'current-page') {
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach ($languages as $language) {
      $tokens_to_process['[current-page:url:relative:' . $language
        ->getId() . ']'] = $language;
    }
    foreach ($tokens as $token) {
      if (isset($tokens_to_process[$token])) {
        $language = $tokens_to_process[$token];
        $url = _external_hreflang_get_url_from_request();
        $url
          ->setOption('language', $language);
        $replacements[$token] = str_replace('/' . $language
          ->getId(), '', $url
          ->toString());
      }
    }
  }
  return $replacements;
}