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.
Comments
Comment #2
damienmckennaIt 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?
Comment #3
andrewsuth commentedIt is a standard Views page that lists nodes of a specific content type.
Comment #4
damienmckennaIs the path for the Views page "it/articoli" or "articoli"? What is the default language of the site?
Comment #5
andrewsuth commentedThe default language of the site is English and the URL of the Views page is
/articlesUsing Pathauto, an alias was created of the Views page in Italian
/articoliwhich then becomes/it/articoliwhen viewed by the user.Comment #6
damienmckennaComment #7
damienmckennaDo these give different results?
Test 1:
* Clear the caches.
* Load /articles.
* Load /it/articoli.
Test 2:
* Clear the caches.
* Load /it/articoli.
Comment #8
andrewsuth commentedIt 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
Comment #9
damienmckennaThanks for testing it, that helps us narrow down what's going.
I think we next need to create a test scenario.
Comment #10
erik_petra commentedI have managed to fix this issue by enabling Use replacement tokens from the first row checkbox.
Comment #11
damienmckennaSo 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?
Comment #14
bbu23I'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:
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.
Comment #15
introfini commentedI had the same issue and the current patch fixed it. Thanks.
Comment #16
damienmckennaSetting the status to "needs review" now that there's a merge request to review.
Comment #17
eugene bocharov commentedThe issue is relevant to webform pages as well. I think it make sense to not extract language from any config entity, not only Views.
Comment #18
damienmckennaI think it'd be worth improving the test coverage to catch this problem.
Comment #19
dimas11 commentedA patch for 2.1.0 version
Comment #20
undersound3 commentedFWIW
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
Comment #21
matdemeueThe 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.
Comment #26
anikettoWe 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
Comment #33
damienmckennaBack to this needing work to add test coverage.
Comment #36
bbu23Comment #37
bbu23Comment #39
nironan commentedIt worked for us. Should the MR be rebased against 2.2.x?
Comment #40
damienmckennaComment #41
damienmckennaThis was an excellent point, so I updated the MR to check for ConfigEntityInterface instead of ViewEntityInterface, hopefully the tests still pass.
Comment #42
damienmckennaComment #43
damienmckennaComment #45
damienmckennaCommitted. Thank you all!