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
- Install Drupal (standard install profile)
- Create a term in the existing "Tags" vocabulary
- View the term page and see that there AREN'T
canonicalorshortlinklinks - Disable the "Taxonomy term (Content)" view
- View the term page and see that there ARE
canonicalandshortlinklinks
Proposed resolution
Revert the removal of taxonomy_page_attachments_alter().
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | after_patch#5.png | 1.06 MB | sonam.chaturvedi |
| #7 | before_patch#5.png | 1.95 MB | sonam.chaturvedi |
| #5 | 3333608-5.patch | 4.23 KB | rocketeerbkw |
| #5 | 3333608-5-fail.patch | 1.05 KB | rocketeerbkw |
Comments
Comment #2
rocketeerbkw commentedI 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 inEntityViewController. 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_1so that it only applies in that specific case, but it seems like the routeentity.taxonomy_term.canonicalis 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.
Comment #3
rocketeerbkw commentedI missed a
usestatement in the previous patch.Comment #4
rocketeerbkw commentedAh, 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.Comment #5
rocketeerbkw commentedNot sure of another way to do this, so adding tests for
/taxonomy/term/{taxonomy_term}with and without views enabled and marking needs review.Comment #7
sonam.chaturvedi commentedVerified 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

After patch

Comment #8
leksat commentedReviewed 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.
Comment #11
idebr commentedComment #12
idebr commentedWhen 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.
Comment #16
alexpottI 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?