Problem/Motivation

When viewing a term page like /taxonomy/term/1, or its alias, the rendered page is missing the links rel="canonical" and rel="shortlink". This is a regression starting at 9.3.0.

This only applies when the view taxonomy_term is enabled, but that means site builders need to pick between having a term page that lists content OR having a term page with canonical links (and therefore better SEO).

Background

In #2922570: The node view page triggers a lot of expensive access checks for relationship links the addition of these links was moved from taxonomy_page_attachments_alter() to the EntityViewController. Since the taxonomy_term view takes over the canonical route and bypasses the term view controller, the links are never added.

Steps to reproduce

  1. Install Drupal (standard install profile)
  2. Create a term in the existing "Tags" vocabulary
  3. View the term page and see that there AREN'T canonical or shortlink links
  4. Disable the "Taxonomy term (Content)" view
  5. View the term page and see that there ARE canonical and shortlink links

Proposed resolution

Revert the removal of taxonomy_page_attachments_alter().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

rocketeerbkw created an issue. See original summary.

rocketeerbkw’s picture

Assigned: rocketeerbkw » Unassigned
Status: Active » Needs work
StatusFileSize
new1.27 KB

I can't see any way to fix this in the term view. Here's a patch that adds back the taxonomy_page_attachments_alter() with a copy of what exists in EntityViewController. Drupal won't render duplicate link tags, so this works with the view enabled and disabled.

I tried to trigger off the view route view.taxonomy_term.page_1 so that it only applies in that specific case, but it seems like the route entity.taxonomy_term.canonical is always returned from the \Drupal::routeMatch() no matter what.

No tests since I'm not sure if this is even the correct method to fix yet.

rocketeerbkw’s picture

StatusFileSize
new1.46 KB

I missed a use statement in the previous patch.

rocketeerbkw’s picture

I tried to trigger off the view route view.taxonomy_term.page_1 so that it only applies in that specific case, but it seems like the route entity.taxonomy_term.canonical is always returned from the \Drupal::routeMatch() no matter what.

Ah, I see now that because the path configured in the taxonomy view (/taxonomy/term/{taxonomy_term}) is already linked to a route (entity.taxonomy_term.canonical), it won't get it's own route from views (view.taxonomy_term.page_1). https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%.... So using a route match won't work to tailor this code to only run when the view is enabled.

rocketeerbkw’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.05 KB
new4.23 KB

Not sure of another way to do this, so adding tests for /taxonomy/term/{taxonomy_term} with and without views enabled and marking needs review.

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

sonam.chaturvedi’s picture

StatusFileSize
new1.95 MB
new1.06 MB

Verified and tested patch #5 on 10.1.x-dev. Patch applied cleanly and successfully.

Test Steps:
1. Create a term in the existing "Tags" vocabulary
2. View the term page and see that there AREN'T canonical or shortlink links on "View Source" page
3. Apply the patch #5
4. View the term page and see that there are canonical or shortlink links on "View Source" page

Test Result: Links rel="canonical" and rel="shortlink" are now displayed on view term page.

Before patch
before patch 5

After patch
after patch 5

leksat’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed the code. Looks good! Two tests make sense.

Nitpicks (not sure if they are needed):

1. If the "fail" patch contained both tests, only one of them would fail. That would demo the need of two tests.

2. If the "without-views" test had a comment describing why it is needed, it could help in the future.

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

idebr’s picture

Title: [Regression] Taxonomy term page missing HTML links canonical and shortlink » [Regression] Taxonomy term page missing HTML links canonical and shortlink when using views
idebr’s picture

When using Views to override any canonical page, these HTML links will be lost. Might be worth fixing for Taxonomy to fix this regression, but I suppose a generic solution for all entity types would be preferable.

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

The last submitted patch, 5: 3333608-5-fail.patch, failed testing. View results

alexpott’s picture

I wonder if we should be seeking a generic fix in views when it takes over the canonical route for an entity. Wouldn't the same thing happen if we used views to take over the canonical route for nodes?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 3333608-5.patch, failed testing. View results

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.