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?