1

Following the Translating content documentation we have setup our site to have Spanish, English, and Japanese. When we are adding, editing, deleting, and publishing pages in different languages we have no issues. However, when we view the canonical url, we find that the language_content_entity=en is added for English pages, for example. We are already adding the hreflang to each page so don't need this extra parameter in the canonical.

For all English pages:

<link rel="canonical" href="https://www.somedomain.net/some/path?language_content_entity=en">

For all Spanish pages:

<link rel="canonical" href="https://www.somedomain.net/some/path?language_content_entity=es"> 

How can we remove the language_content_entity=en from the canonical url?

1

Just to illustrate once again what you are supposed to do with the first duplicate question's answer:

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

  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) {
        // Strip ?language_content_entity=en from the end of the URL if exists.
        $href = rtrim($href, '?language_content_entity=en');
        // Strip ?language_content_entity=es from the end of the URL if exists.
        $href = rtrim($href, '?language_content_entity=es');
        // Then set the new value.
        $attachments['#attached']['html_head'][$key][0]['#attributes']['href'] = $href;
      }
    }
  }
}
  • this may work for small set of languages, but the moment we go with more than languages i would have to add more lines with $href = rtrim($href, 'language');. Let me test a regex to see if i can get this to work. – usernameabc Oct 23 '18 at 13:39
  • @usernameabc – That is up to you. Or you find a different way how to avoid the query gets added in the first place. And use a different logic maybe to fire certain features that are bound to this query. You asked for how to strip a certain string from the canonical URL. And this is the answer. :-) – leymannx Oct 23 '18 at 13:40
  • @usernameabc – Or you provide your own custom tokens. But then you also probably will end up with a similar pattern. – leymannx Oct 23 '18 at 13:46

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.