My basic apprach for 7.x-2.x is (assume the module is called mymodule_xmlsitemap).
/**
* Implements hook_cron().
*/
function mymodule_xmlsitemap_cron() {
mymodule_xmlsitemap_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}
/**
* Implements hook_xmlsitemap_index_links().
*/
function mymodule_xmlsitemap_xmlsitemap_index_links($limit) {
xmlsitemap_switch_user(0);
xmlsitemap_link_delete_multiple(array(
'type' => 'mymodule',
));
$results = mymodule_query();
foreach ($results as $result) {
$link = _mymodule_xmlsitemap_make_link($result->path, $result->id);
xmlsitemap_link_save($link, $result->id);
}
xmlsitemap_restore_user();
}
/**
* Helper for link object creation.
*/
function _mymodule_xmlsitemap_make_link($path, $id) {
return array(
'loc' => $path,
'type' => 'mymodule',
'id' => $id,
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
'lastmod' => 0,
'changefreq' => 0,
'changecount' => 0,
'language' => LANGUAGE_NONE,
);
}
As you mentioned, hook_xmlsitemap_index_links
doesn't get invoked unless you have things defined with hook_xmlsitemap_link_info
. In the 7.x version, I didn't see how to divorce this from database tables. If you are truly coming from an external database, this may not be possible.
So, if you look at xmlsitemap_node (that was the inspiration for my version), you will see that the hook_xmlsitemap_index_links()
is manually called from hook_cron()
. With an external database, I had no idea on whether anything was new or deleted, so I blasted out all of the links, and rebuilt them. In my case, I ignore the $limit
. You then make you query, and save off links.
Also note that there is some user switching going on to ensure that you are only indexing things that the anonymous user can access.
If you get indication when things are added / deleted from your external database, you can get rid of the hook_xmlsitemap_index_links()
, and implement your own callbacks to xmlsitemap_link_save()
and xmlsitemap_link_delete()
.