1

I have a requirement to use absolute URLs for the canonical URL element generated by Drupal 8.

hook_page_attachments_alter doesn't seem to have access the HTML element. Is there any other way I can access it?

2

True, for nodes you can't use hook_page_attachments_alter. You have to use hook_ENTITY_TYPE_view_alter!

And very likely you also find all of them in the end in hook_preprocess_html.

Here's how I got it working:

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function MYMODULE_node_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  if (isset($build['#attached']['html_head_link'])) {
    foreach ($build['#attached']['html_head_link'] as $key => $head) {
      if ((isset($head[0]['rel']) ? $head[0]['rel'] : FALSE) == 'canonical') {

        $url = \Drupal\Core\Url::fromRoute('<current>', [], ['absolute' => 'true'])
          ->toString();

        $build['#attached']['html_head_link'][$key][0]['href'] = $url;
      }
    }
  };
}

Source: How to remove specific meta-tag from header section of page?


Alternatively you can use Metatag. All you have to do is to set the [node:url:absolute] token into the Canonical URL form field in the Advanced section of the Metatag configuration for all Content.

enter image description here

And only then, all of sudden, they become available to be preprocessed in hook_page_attachments_alter. There I removed their trailing slash which gets added by default.

/**
 * Implements hook_page_attachments_alter().
 */
function MYTHEME/MYMODULE_page_attachments_alter(array &$attachments) {

  // Remove trailing slash from canonicals.
  foreach ($attachments['#attached']['html_head'] as $key => $head) {

    if ((isset($head[1]) ? $head[1] : FALSE) == 'canonical_url') {

      $href = isset($head[0]['#attributes']['href']) ? $head[0]['#attributes']['href'] : FALSE;

      if ($href) {
        $attachments['#attached']['html_head'][$key][0]['#attributes']['href'] = rtrim($href, '/');
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.