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.

Issue fork metatag-3302453

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andrewsuth created an issue. See original summary.

damienmckenna’s picture

Component: Multilingual/intl » Views integration
Issue tags: +Needs tests

It is likely a result of this change: #3077442

In order to change this further we'd need to have test coverage.

Is this a standalone Views page or is it connected to an entity?

andrewsuth’s picture

It is a standard Views page that lists nodes of a specific content type.

damienmckenna’s picture

Is the path for the Views page "it/articoli" or "articoli"? What is the default language of the site?

andrewsuth’s picture

The default language of the site is English and the URL of the Views page is /articles

Using Pathauto, an alias was created of the Views page in Italian /articoli which then becomes /it/articoli when viewed by the user.

damienmckenna’s picture

Do these give different results?

Test 1:
* Clear the caches.
* Load /articles.
* Load /it/articoli.

Test 2:
* Clear the caches.
* Load /it/articoli.

andrewsuth’s picture

It doesn't appear to be a caching problem. Below are the results of both tests:

Test 1:
* Clear the caches.
* Load /articles
- Result: canonical correctly uses to the current URL /articles
* Load /it/articoli.
- Result: canonical incorrectly uses the English URL /articles

Test 2:
* Clear the caches.
* Load /it/articoli.
- Result: canonical incorrectly uses the English URL /articles

damienmckenna’s picture

Thanks for testing it, that helps us narrow down what's going.

I think we next need to create a test scenario.

erik_petra’s picture

I have managed to fix this issue by enabling Use replacement tokens from the first row checkbox.

  1. Go to settings of a view
  2. Open Meta tags settings
  3. Enable checkbox Use replacement tokens from the first row
damienmckenna’s picture

Issue tags: -Needs tests

So maybe we need some hints in the UI to enable that option?

Alternatively, maybe this should be automatic? Or maybe the option should be enabled by default, with a hint to disable it for performance reasons if it isn't needed?

bbu23 made their first commit to this issue’s fork.

bbu23’s picture

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 and could break other stuff that I'm not aware of, 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.

introfini’s picture

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

damienmckenna’s picture

Status: Active » Needs review

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

eugene bocharov’s picture

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.

damienmckenna’s picture

Version: 8.x-1.21 » 2.0.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests

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

dimas11’s picture

StatusFileSize
new770 bytes

A patch for 2.1.0 version

undersound3’s picture

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.

Metatag 2.1.0
Drupal 10.4.4

matdemeue’s picture

Version: 2.0.x-dev » 2.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.54 KB

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.

aniketto made their first commit to this issue’s fork.

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

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

aniketto’s picture

Status: Needs review » Reviewed & tested by the community

We discussed this issue as part of [Drupal Contribution Day Japan 2025](https://www.drupal.org/community/events/drupal-japan-contribution-day-ju...)

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

Drupal.org usernames:
- yukihyogo
- gengo_k
- bassline

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

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

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

damienmckenna’s picture

Status: Reviewed & tested by the community » Needs work

Back to this needing work to add test coverage.

damienmckenna changed the visibility of the branch 8.x-1.x to hidden.

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

bbu23’s picture

Status: Needs work » Needs review
bbu23’s picture

Issue tags: -Needs tests

nironan’s picture

Status: Needs review » Reviewed & tested by the community

It worked for us. Should the MR be rebased against 2.2.x?

damienmckenna’s picture

Version: 2.1.x-dev » 2.2.x-dev
damienmckenna’s picture

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.

This was an excellent point, so I updated the MR to check for ConfigEntityInterface instead of ViewEntityInterface, hopefully the tests still pass.

damienmckenna’s picture

damienmckenna’s picture

Title: Canonical metatag on Views pages no longer refers to current, language page » Canonical meta tag on config entity pages (e.g. Views) no longer refers to current language

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thank you all!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.