function content_translation_page_attachments

Same name in other branches
  1. 9 core/modules/content_translation/content_translation.module \content_translation_page_attachments()
  2. 8.9.x core/modules/content_translation/content_translation.module \content_translation_page_attachments()
  3. 11.x core/modules/content_translation/content_translation.module \content_translation_page_attachments()

Implements hook_page_attachments().

File

core/modules/content_translation/content_translation.module, line 668

Code

function content_translation_page_attachments(&$page) {
    $cache = CacheableMetadata::createFromRenderArray($page);
    $route_match = \Drupal::routeMatch();
    // If the current route has no parameters, return.
    if (!($route = $route_match->getRouteObject()) || !($parameters = $route->getOption('parameters'))) {
        return;
    }
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
    // Determine if the current route represents an entity.
    foreach ($parameters as $name => $options) {
        if (!isset($options['type']) || !str_starts_with($options['type'], 'entity:')) {
            continue;
        }
        $entity = $route_match->getParameter($name);
        if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
            // Current route represents a content entity. Build hreflang links.
            foreach ($entity->getTranslationLanguages() as $language) {
                // Skip any translation that cannot be viewed.
                $translation = $entity->getTranslation($language->getId());
                $access = $translation->access('view', NULL, TRUE);
                $cache->addCacheableDependency($access);
                if (!$access->isAllowed()) {
                    continue;
                }
                if ($is_front) {
                    // If the current page is front page, do not create hreflang links
                    // from the entity route, just add the languages to root path.
                    $url = Url::fromRoute('<front>', [], [
                        'absolute' => TRUE,
                        'language' => $language,
                    ])->toString();
                }
                else {
                    $url = $entity->toUrl('canonical')
                        ->setOption('language', $language)
                        ->setAbsolute()
                        ->toString();
                }
                $page['#attached']['html_head_link'][] = [
                    [
                        'rel' => 'alternate',
                        'hreflang' => $language->getId(),
                        'href' => $url,
                    ],
                ];
            }
        }
        // Since entity was found, no need to iterate further.
        break;
    }
    // Apply updated caching information.
    $cache->applyTo($page);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.