API and extending the module

Last updated on
22 March 2024

API

There are API methods for altering stored inclusion settings, status queries and programmatic sitemap generation. These include:

  • simple_sitemap.generator
    • setSitemaps
    • getSitemaps
    • getDefaultSitemap
    • getSetting
    • saveSetting
    • getContent
    • generate
    • queue
    • rebuildQueue
    • entityManager
      • setSitemaps
      • getSitemaps
      • enableEntityType
      • disableEntityType
      • setBundleSettings
      • getBundleSettings
      • getAllBundleSettings
      • removeBundleSettings
      • setEntityInstanceSettings
      • getEntityInstanceSettings
      • removeEntityInstanceSettings
      • bundleIsIndexed
      • entityTypeIsEnabled
    • customLinkManager
      • setSitemaps
      • getSitemaps
      • add
      • get
      • remove

These service methods can be chained like so:

// Create a new sitemap 'test' of the default_hreflang sitemap type.
\Drupal\simple_sitemap\Entity\SimpleSitemap::create(['id' => 'test', 'type' => 'default_hreflang', 'label' => 'Test'])->save();

/** @var \Drupal\simple_sitemap\Manager\Generator $generator */
$generator = \Drupal::service('simple_sitemap.generator');

// Set some random settings (global, not sitemap specific).
if ($generator->getSetting('cron_generate')) {
  $generator
    ->saveSetting('generate_duration', 20000)
    ->saveSetting('base_url', 'https://test');
}

// Set an entity bundle to be indexed in the 'default' and 'test' sitemaps.
$generator
  ->entityManager()
  ->enableEntityType('node')
  ->setSitemaps(['default', 'test']) // All following operations will concern these sitemaps.
  ->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' => 0.5]);

// Remove all custom links from the 'default' and 'test' sitemaps and Set a
// custom link to be indexed in the 'test' sitemap.
$generator
  ->customLinkManager()
  ->remove() // Remove all custom links from all sitemaps.
  ->setSitemaps(['test']) // All following operations will concern these sitemaps.
  ->add('/some/view/page', ['priority' => 0.5]);

// Queues the 'test' sitemap for generation and generates it.
$generator
  ->rebuildQueue()
  ->generate();

To query data of and manipulate a specific sitemap, load it and use its various methods. Some arbitrary example:

$sitemap = \Drupal\simple_sitemap\Entity\SimpleSitemap::load('default');
// Be aware, that $sitemap->status() only returns TRUE if the sitemap is enabled
// and published. To check if it is enabled only, use $sitemap->isEnabled().
if ($sitemap->status() && !$sitemap->isDefault() && $sitemap->getCreated() < $some_timestamp) {
  $sitemap->disable();

See https://gbyte.dev/projects/simple-xml-sitemap and code documentation for further details.

API HOOKS

It is possible to hook into link generation by implementing hook_simple_sitemap_links_alter(&$links, $sitemap){} in a custom module and altering the link array shortly before it is transformed to XML.

Adding arbitrary links is possible through the use of hook_simple_sitemap_arbitrary_links_alter(&$arbitrary_links, $sitemap){}. 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.

Altering sitemap attributes and sitemap index attributes is possible through the use of hook_simple_sitemap_attributes_alter(&$attributes, $sitemap){} and hook_simple_sitemap_index_attributes_alter(&$index_attributes, $sitemap){}.

Altering URL generators is possible through the use of hook_simple_sitemap_url_generators_alter(&$url_generators){}.

Altering sitemap generators is possible through the use of hook_simple_sitemap_sitemap_generators_alter(&$sitemap_generators){}.

Sitemaps as well as sitemap types can be altered through the usual entity hooks.

WRITING PLUGINS

There are two 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 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 a sitemap type.

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.

Help improve this page

Page status: No known problems

You can: