Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When we remove links on hook_simple_sitemap_links_alter it doesn't update pagination.
Steps
- We have 100 pages indexed on sitemap.xml
- We have configured only 10 links per page
- There are 10 sitemap.xml with 10 links each (total = 100, okay it works well)
Problem
We created a hook_simple_sitemap_links_alter, and we're removing 80 links (total = 20)
Example:
function PROJECT_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
foreach ($links as $key => $link) {
if ($key >= 20 ) {
unset($links[$key]);
continue;
}
}
}
Espected result
Should be 2 sitemap.xml with 10 links each
Actual
There are 10 sitemap.xml
- 1st sitemap with 10 links
- 2st sitemap.xml with 10 links
- 3rd sitemap in blank
- 4th sitemap in blank
- 5th sitemap in blank
- 6th sitemap in blank
- 7th sitemap in blank
- 8th sitemap in blank
- 9th sitemap in blank
- 10th sitemap in blank
Question
There is a solution to "remove" links and update pagination?
Thanks in advanced
Comment | File | Size | Author |
---|---|---|---|
#4 | 3153405.patch | 573 bytes | renatog |
Comments
Comment #2
renatogComment #3
renatogComment #4
renatogThis is a patch with a solution
If all modules alter $results will fix this problem automatically.
Follow the patch for 3.7
Comment #5
renatogComment #6
pratik_kambleComment #7
pratik_kamble@RenatoG, I am not able to reproduce the issue. I have configured the simple_sitemap module to contain only 10 links per page for sitemap.xml.
I had a total of 280 content.
Added custom code as suggested by you.
Regenerated the links by clicking on
Regenerate all sitemaps after hitting Save.
It still shows 28 pagers with all the links.When I debugged the code I found that key value was always below 10 so the if condition was never true.
@RenatoG I will need more input to review it further.
Comment #8
renatogHello @pratik_kamble
Have you changed "PROJECT" key by your project key that's right?
If yes, and you have 208 links, the link number 1 will be
So in the end you'll have only 20 links indexed in the sitemap, but you'll have a lot of os pages with other sitemal.xml (in blank)
My patch #4 implements result alter, and if we tested the same approach but with a different hook works well
Using this new hook in the final we'll receive a correct pagination
THanks a lot
Comment #9
gbyte CreditAttribution: gbyte as a volunteer and at gbyte commentedThere is a problem indeed, I don't like #4 however because the hook would fire for every single link but would operate on links it operated on in the previous iteration. I am going to look into it.
Comment #10
renatogYes, exactly. I checked it as well.
With #4 the hook is called many times
That's great! Any solution please let us know. Thank you so much for your help
Comment #12
gbyte CreditAttribution: gbyte as a volunteer and at gbyte commentedThis took much longer than anticipated. The problem is that the results are stashed in between of generation instances and we have to stash the altered results as well as the original ones and make sure elements are processed once. At the same time we don't want each element to be processed by itself because of the diminished performance that would cause.
I think I found the cleanest way to fix the problem, please test the dev version and report back.
Comment #13
renatogOkay, I'll test it
Thank you so much for your help
Comment #14
renatogConfirmed and really works well!!
Thank you so much and congrats on your job!
@gbyte.co, please can you generate a new release 8.x-3.8 with this fix?
It's important and uses a stable version is better
Have a good weekend
Comment #15
gbyte CreditAttribution: gbyte as a volunteer and at gbyte commentedHappy it works for you.
If it was really that important, it would have been discovered ages ago. ;)
Sorry, I just released 3.7, 3.8 will have to wait for a couple of bugixes/features. But you can deploy the snapshot of the dev as-is, as right now it's 3.7 with this fix only. I believe you can do it like so:
composer require drupal/simple_sitemap:3.x-dev#59514f24295e575f0f4fc76eab9d4f03aa30f73d
Weekend just ended here... :/