2

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?

9
  • When you use the language detection method Session and a user doesn't have a session, the detection falls back to a query parameter.
    – 4uk4
    Oct 22, 2018 at 20:27
  • The method "Content language" adds a query parameter, too, which seems to be the one you have in your case.
    – 4uk4
    Oct 22, 2018 at 20:44
  • Possible duplicate of Make canonical URL in document HEAD absolute
    – leymannx
    Oct 23, 2018 at 7:35
  • Possible duplicate of How to remove specific meta-tag from header section of page?
    – leymannx
    Oct 23, 2018 at 7:36
  • 1
    I vote to close the question because it is unclear whether it is about altering metatags in general or about a specific query string added to outbound urls.
    – 4uk4
    Oct 23, 2018 at 18:22

1 Answer 1

2

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;
      }
    }
  }
}
5
  • 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. Oct 23, 2018 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, 2018 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, 2018 at 13:46
  • 1
    Note that the question is asking specifically about removing language_content_entity query string. The best way to do that is to change the order of the language negotiation mechanism (see @4k4 comment in question). The proposed solution here may work but is far from ideal
    – Pere
    Jul 29, 2020 at 8:14
  • 1
    @Pere – Ah yeah, I see. I was misguided by the metatag question tag. You should consider adding a proper answer that explains how to do that exactly.
    – leymannx
    Jul 29, 2020 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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