function taxonomy_page_attachments_alter

taxonomy_page_attachments_alter(array &$page)

Implements hook_page_attachments_alter().

File

core/modules/taxonomy/taxonomy.module, line 92
Enables the organization of content into categories.

Code

function taxonomy_page_attachments_alter(array &$page) {
  $route_match = \Drupal::routeMatch();
  if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) {
    foreach ($term->uriRelationships() as $rel) {
      // Set the URI relationships, like canonical.
      $page['#attached']['html_head_link'][] = array(
        array(
          'rel' => $rel,
          'href' => $term->url($rel),
        ),
        TRUE,
      );

      // Set the term path as the canonical URL to prevent duplicate content.
      if ($rel == 'canonical') {
        // Set the non-aliased canonical path as a default shortlink.
        $page['#attached']['html_head_link'][] = array(
          array(
            'rel' => 'shortlink',
            'href' => $term->url($rel, array('alias' => TRUE)),
          ),
          TRUE,
        );
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!taxonomy!taxonomy.module/function/taxonomy_page_attachments_alter/8.1.x