Canonical metatag on Views pages no longer refers to current, language page

Created on 8 August 2022, about 3 years ago
Updated 22 February 2023, over 2 years ago

Problem/Motivation

Since upgrading from Metatag 1.19 to 1.21, the metatag for the canonical link in Views pages no longer refers to the current language page (Italian) and instead it refers to the original language page (English).

For example, on the page example.com/it/articoli, this is the HTML diff since upgrading to Metatags 1.21

-<link rel="canonical" href="https://example.com/it/articoli" />
+<link rel="canonical" href="https://example.com/articles" />
-<meta property="og:url" content="https://example.com/it/articoli" />
+<meta property="og:url" content="https://example.com/articles" />
-<meta name="twitter:url" content="https://example.com/it/articoli" />
+<meta name="twitter:url" content="https://example.com/articles" />

This issue only affect Views pages which are in different languages from the default language of the website.

🐛 Bug report
Status

Active

Version

1.21

Component

Views integration

Created by

🇮🇹Italy andrewsuth

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • 🇷🇴Romania bbu23

    I've just encountered this issue on some views pages on a multilingual site. I arrived at line 591 in web/modules/contrib/metatag/src/MetatagManager.php file:

        // Use the entity's language code, if one is defined.
        $langcode = NULL;
        if ($entity) {
          $langcode = $entity->language()->getId();
        }
    

    The language code that is later used in all token replacement calls seems to be declared here, and the $entity variable contains the Views configuration object. This object always returns the default language, at least for my case.

    I added a patch that might not be the best fix/approach, but I think it's good to start somewhere. I'm omitting to set the current language if the $entity is a view (probably it applies to all config entities, but idk about that) so that later on in the execution the language is set with the current detected language. We don't want to have the language of the config object, but the interface language (especially for views pages).

    I tried the option from comment #10, but that didn't work for me.

  • 🇵🇹Portugal introfini

    I had the same issue and the current patch fixed it. Thanks.

  • Status changed to Needs review over 2 years ago
  • 🇺🇸United States damienmckenna NH, USA

    Setting the status to "needs review" now that there's a merge request to review.

  • The issue is relevant to webform pages as well. I think it make sense to not extract language from any config entity, not only Views.

  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States damienmckenna NH, USA

    I think it'd be worth improving the test coverage to catch this problem.

  • A patch for 2.1.0 version

  • 🇳🇱Netherlands undersound3

    FWIW

    When I enter [view:url] in the Canonical-URL field under Metatag settings in the view I managed to get a correct url for both languagaes in a multilanguage domain based setup

  • Status changed to Needs review 2 months ago
  • 🇧🇪Belgium matdemeue

    The current patch from #19 didn't fix it for us. In issue #3077442 the generateRawElements function is also updated.

    I rerolled it with the same fix in this function as well to cover both cases. This solves the problem in our case where rawElements are used. Review needed.

  • First commit to issue fork.
  • 🇯🇵Japan aniketto Tokyo

    aniketto changed the visibility of the branch 3302453-canonical-metatag-on-for-2-1 to hidden.

  • 🇯🇵Japan aniketto Tokyo

    aniketto changed the visibility of the branch 3302453-canonical-metatag-on to hidden.

  • 🇯🇵Japan aniketto Tokyo

    Patch #21 worked. Attaching before/after screenshots.
    Also worked on creating the merge-request for 2.1 branch using the same patch.

    This issue was picked up as part of Contribution Day Japan 2025 held on 11th July, 2025 at Tokyo.
    Below members helped me to confirm the changes and re-create the merge-request.

    Members: yukihyogo, gengo_k, bassline

  • Pipeline finished with Failed
    about 1 month ago
    Total: 1257s
    #544746
  • 🇯🇵Japan aniketto Tokyo

    aniketto changed the visibility of the branch 3302453-canonical-metatag-on-2.1 to hidden.

  • 🇯🇵Japan aniketto Tokyo

    aniketto changed the visibility of the branch 3302453-canonical-metatag-on to active.

  • 🇯🇵Japan aniketto Tokyo

    aniketto changed the visibility of the branch 2.1.x to hidden.

Production build 0.71.5 2024