Skip to content

Instantly share code, notes, and snippets.

@ianlintner-wf
Last active November 11, 2016 19:12
Show Gist options
  • Save ianlintner-wf/ce83d50764f138a087f1901760c42377 to your computer and use it in GitHub Desktop.
Save ianlintner-wf/ce83d50764f138a087f1901760c42377 to your computer and use it in GitHub Desktop.
Drupal 7 XMLSitemap Module: Create a Sitemap for a language programmatically.
/**
* Create a site map for a specific language.
*
* @param string $language
* Language code.
*
* @return bool
* Success bool.
*/
function utility_xmlsitemap_create_sitemap($language) {
include_once drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.admin.inc';
// This is how the module creates the data. I don't know if it is an entity.
$query = db_select('xmlsitemap_sitemap');
$query->fields('xmlsitemap_sitemap');
$result = $query->execute();
$site_maps = $result->fetchAllAssoc('smid');
foreach ((array) $site_maps as $key => $value) {
$current_context = unserialize($value->context);
if (strtolower($current_context['language']) === strtolower($language)) {
// If it exists do not create.
watchdog('utility_xmlsitemap_create_sitemap',
'Site map exists and will not be created for language: :language',
array(':language' => $language),
WATCHDOG_NOTICE
);
return FALSE;
}
}
watchdog('utility_xmlsitemap_create_sitemap',
'Site map does not exist language: :language',
array(':language' => $language),
WATCHDOG_INFO
);
// Create a site map object for the specified language.
$site_map = new stdClass();
$site_map->smid = NULL;
$site_map->context = array('language' => $language);
xmlsitemap_sitemap_save($site_map);
watchdog('utility_xmlsitemap_create_sitemap',
'Created site map for language: :language',
array(':language' => $language),
WATCHDOG_INFO
);
return TRUE;
}
@ianlintner-wf
Copy link
Author

This is a function demonstrating how to programmatically create an XML sitemap in Drupal 7 using the xmlsitemap contrib module.

This is suitable for use in devel as one time script or in a custom site module install file to create a sitemap on deploy of a new language since I don't believe they are feature-ized.

If needed you could add validation around language codes, but since these are one time functions many times and this is a proof of concept I left it out... or I am just being lazy lol.

utility_xmlsitemap_create_sitemap('en-US');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment