Multiple sitemap URLs

Created on 30 June 2023, over 1 year ago
Updated 8 January 2024, 11 months ago

I need to create multiple sitemaps with unique URLs.

My goal is to segment the content in my site into sitemaps by URL prefix, though there are any number of other use-cases where this might be important based on entity type or bundle or other kinds of criteria.

I need multiple sitemaps because I have subdomains running other applications that deliver content stored in Drupal, and it's easier to rely on xmlsitemap than have those other applications build their own sitemap.

My question is: how do I hook into the API to differentiate the sitemap URLs?

If I could specify the URL indicator using a keyword like "apples", then here are possible sitemap URL schemes that I would be willing to work with:

So far, here are the hooks I have implemented:

  • hook_xmlsitemap_context_info() to list my context.
  • hook_xmlsitemap_form_xmlsitemap_sitemap_edit_form_alter() to add my context selector when editing a sitemap
  • hook_xmlsitemap_xmlsitemap_context() to check if the current URL matches a context
  • hook_xmlsitemap_context_url_options() to alter the URLs of my sitemaps based on context
  • hook_xmlsitemap_query_xmlsitemap_generate_alter() to select which content to include in the sitemap

The closest I came to solving this was to use hook_xmlsitemap_xmlsitemap_context_url_options() to append a querystring for the content type, e.g. sitemap.xml?type=apples. That code looks like:
$options['query'] = array('type' => 'apples');

However, that approach doesn't work when the sitemap is chunked into multiple pages because of how $options is merged in XMLSitemapWriter->getSitemapUrl(). It has the code $options += $this->sitemap->uri['options']; which only adds my query param "type => apples" if the "page" param doesn't exist. If instead the code was written as:
$options = array_merge_recursive($options, $this->sitemap->uri['options']);
then it would include both "page" and my custom query param "type".

So... what's the correct way to have multiple sitemap URLs depending on context?

a ) Fix the code above to use array_merge_recursive?
b ) Don't use $options['query'] in hook_xmlsitemap_xmlsitemap_context_url_options() and instead override some other param like $options['base_url']?
c ) Some other idea I haven't come across yet?

💬 Support request
Status

Active

Version

2.7

Component

Code

Created by

🇺🇸United States hargobind Austin, Texas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @hargobind
  • 🇺🇸United States hargobind Austin, Texas

    For now, I am providing a patch which seems to work as a fix for the array_merge_recursive() approach mentioned above in case anyone else finds it useful.

    But I'd still love to hear from maintainers or anyone else who knows if there is a better way to solve this.

  • 🇮🇳India ganesh_hegde

    I would like to know if someone has tried the above patch and has found it helpful. I have a similar used case, but I need it for the latest version of Drupal.

Production build 0.71.5 2024