2 deleted 79 characters in body
source | link

Drupal 8

For nodes you have to use hook_ENTITY_TYPE_view_alter as that's the place where they got added to initially from NodeViewController::view().

And let me note that you are probably better off by simply redirecting all your incoming traffic to SSL by default: How to simply make the whole site HTTPS?

/**
 * 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();

        $url = str_replace('https://', 'http://', $url);

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

ForI just found out, that in the end we'll find all remaining canonicals you might need to look intothe head tags in hook_page_attachments_alterhook_preprocess_html in the $variables['page']['#attached'] array to be altered.

Drupal 8

For nodes you have to use hook_ENTITY_TYPE_view_alter as that's the place where they got added to initially from NodeViewController::view().

And let me note that you are probably better off by simply redirecting all your incoming traffic to SSL by default: How to simply make the whole site HTTPS?

/**
 * 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();

        $url = str_replace('https://', 'http://', $url);

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

For all remaining canonicals you might need to look into hook_page_attachments_alter.

Drupal 8

For nodes you have to use hook_ENTITY_TYPE_view_alter as that's the place where they got added to initially from NodeViewController::view().

And let me note that you are probably better off by simply redirecting all your incoming traffic to SSL by default: How to simply make the whole site HTTPS?

/**
 * 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();

        $url = str_replace('https://', 'http://', $url);

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

I just found out, that in the end we'll find all the head tags in hook_preprocess_html in the $variables['page']['#attached'] array to be altered.

1
source | link

Drupal 8

For nodes you have to use hook_ENTITY_TYPE_view_alter as that's the place where they got added to initially from NodeViewController::view().

And let me note that you are probably better off by simply redirecting all your incoming traffic to SSL by default: How to simply make the whole site HTTPS?

/**
 * 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();

        $url = str_replace('https://', 'http://', $url);

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

For all remaining canonicals you might need to look into hook_page_attachments_alter.