API and extending the module
API
There are API methods for altering stored inclusion settings, status queries and programmatic sitemap generation. These include:
- getSetting
- saveSetting
- setVariants
- getSitemap
- removeSitemap
- generateSitemap
- queue
- rebuildQueue
- generateSitemap
- enableEntityType
- disableEntityType
- setBundleSettings
- getBundleSettings
- removeBundleSettings
- setEntityInstanceSettings
- getEntityInstanceSettings
- removeEntityInstanceSettings
- bundleIsIndexed
- entityTypeIsEnabled
- addCustomLink
- getCustomLinks
- removeCustomLinks
- getSitemapManager
- getSitemapVariants
- addSitemapVariant
- removeSitemapVariants
- getQueueWorker
- deleteQueue
- getInitialElementCount
- getQueuedElementCount
- getStashedResultCount
- getProcessedElementCount
- generationInProgress
These service methods can be chained like so:
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->getSitemapManager()
->addSitemapVariant('test');
$generator
->saveSetting('remove_duplicates', TRUE)
->enableEntityType('node')
->setVariants(['default', 'test'])
->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5])
->removeCustomLinks()
->addCustomLink('/some/view/page', ['priority' => 0.5])
->generateSitemap();
See https://gbyte.co/projects/simple-xml-sitemap and code documentation in Drupal\simple_sitemap\Simplesitemap for further details.
Extending the module
API hooks
It is possible to hook into link generation by implementing hook_simple_sitemap_links_alter(&$links, $sitemap_variant){} in a custom module and altering the link array shortly before it is transformed to XML.:
/**
* Alter the generated link data before the sitemap is saved.
* This hook gets invoked for every sitemap chunk generated.
*
* @param array &$links
* Array containing multilingual links generated for each path to be indexed
*
* @param string|null $sitemap_variant
*/
function hook_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
// Remove German URL for a certain path in the hreflang sitemap.
foreach ($links as $key => $link) {
if ($link['meta']['path'] === 'node/1') {
// Remove 'loc' URL if it points to a german site.
if ($link['langcode'] === 'de') {
unset($links[$key]);
}
// If this 'loc' URL points to a non-german site, make sure to remove
// its german alternate URL.
else {
if ($link['alternate_urls']['de']) {
unset($links[$key]['alternate_urls']['de']);
}
}
}
}
}
Adding arbitrary links is possible through the use of hook_simple_sitemap_arbitrary_links_alter(&$arbitrary_links, $sitemap_variant){}. There are no checks performed on these links (i.e. if they are internal/valid/accessible) and parameters like priority/lastmod/changefreq have to be added manually:
/**
* Add arbitrary links to the sitemap.
*
* @param array &$arbitrary_links
* @param string|null $sitemap_variant
*/
function hook_simple_sitemap_arbitrary_links_alter(array &$arbitrary_links, $sitemap_variant) {
// Add an arbitrary link to all sitemap variants.
$arbitrary_links[] = [
'url' => 'http://some-arbitrary-link/',
'priority' => '0.5',
// An ISO8601 formatted date.
'lastmod' => '2012-10-12T17:40:30+02:00',
'changefreq' => 'weekly',
'images' => [
['path' => 'http://path-to-image.png']
],
// Add alternate URLs for every language of a multilingual site.
// Not necessary for monolingual sites.
'alternate_urls' => [
'en' => 'http://this-is-your-life.net/de/tyler',
'de' => 'http://this-is-your-life.net/en/tyler',
]
];
// Add an arbitrary link to the 'fight_club' sitemap variant only.
switch ($sitemap_variant) {
case 'fight_club':
$arbitrary_links[] = [
'url' => 'http://this-is-your-life.net/tyler',
];
break;
}
}
Altering sitemap attributes is possible through the use of hook_simple_sitemap_attributes_alter(&$attributes, $sitemap_variant){}:
/**
* Alters the sitemap attributes shortly before XML document generation.
* Attributes can be added, changed and removed.
*
* @param array &$attributes
* @param string|null $sitemap_variant
*/
function hook_simple_sitemap_attributes_alter(array &$attributes, $sitemap_variant) {
// Remove the xhtml attribute e.g. if no xhtml sitemap elements are present.
unset($attributes['xmlns:xhtml']);
}
Altering sitemap index attributes is possible through the use of hook_simple_sitemap_index_attributes_alter(&$index_attributes, $sitemap_variant){}:
/**
* Alters attributes of the sitemap index shortly before XML document generation.
* Attributes can be added, changed and removed.
*
* @param array &$index_attributes
* @param string|null $sitemap_variant
*/
function hook_simple_sitemap_index_attributes_alter(array &$index_attributes, $sitemap_variant) {
// Add some attribute to the sitemap index.
$index_attributes['name'] = 'value';
}
Altering URL generator plugins is possible through the use of hook_simple_sitemap_url_generators_alter(&$generators){}:
/**
* Alter properties of and remove URL generator plugins.
*
* @param array $url_generators
*/
function hook_simple_sitemap_url_generators_alter(array &$url_generators) {
// Remove the entity generator.
unset($url_generators['entity']);
}
Altering sitemap generator plugins is possible through the use of hook_simple_sitemap_sitemap_generators_alter(&$sitemap_generators){}:
/**
* Alter properties of and remove sitemap generator plugins.
*
* @param array $sitemap_generators
*/
function hook_simple_sitemap_sitemap_generators_alter(array &$sitemap_generators) {
// Remove the default generator.
unset($sitemap_generators['default']);
}
Altering sitemap types is possible through the use of hook_simple_sitemap_sitemap_types_alter(&$sitemap_types){}:
/**
* Alter properties of and remove sitemap type plugins.
*
* @param array $sitemap_types
*/
function hook_simple_sitemap_sitemap_types_alter(array &$sitemap_types) {
}
Writing plugins
There are three types of plugins that allow to create any type of sitemap. See the generator plugins included in this module and check the API docs (https://www.drupal.org/docs/8/api/plugin-api/plugin-api-overview) to learn how to implement plugins.
Sitemap type plugins
This plugin defines a sitemap type. A sitemap type consists of a sitemap generator and several URL generators. This plugin is very simple, as it only requires some class annotation to define which sitemap/URL plugins to use
Sitemap generator plugins
This plugin defines how a sitemap type is supposed to look. It handles all aspects of the sitemap except its links/URLs.
URL generator plugins
This plugin defines a way of generating URLs for one or more sitemap types.
Note:
Overwriting the default EntityUrlGenerator for a single entity type is possible through the flag "overrides_entity_type" = "[entity_type_to_be_overwritten]" in the settings array of the new generator plugin's annotation. See how the EntityUrlGenerator is overwritten by the EntityMenuLinkContentUrlGenerator to facilitate a different logic for menu links.
See https://gbyte.dev/projects/simple-xml-sitemap for further details.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion