API and extending the module

Last updated on
12 December 2017

API

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

  • getSetting()
  • saveSetting()
  • getSitemap()
  • generateSitemap()
  • getGeneratedAgo()
  • enableEntityType()
  • disableEntityType()
  • setBundleSettings()
  • getBundleSettings()
  • setEntityInstanceSettings()
  • getEntityInstanceSettings()
  • removeEntityInstanceSettings()
  • bundleIsIndexed()
  • entityTypeIsEnabled()
  • addCustomLink()
  • getCustomLinks()
  • getCustomLink()
  • removeCustomLink()
  • removeCustomLinks()

These service methods can be chained like so:

\Drupal::service('simple_sitemap.generator')
  ->saveSetting('remove_duplicates', TRUE)
  ->enableEntityType('node')
  ->setBundleSettings('node', 'page', ['index' => TRUE, 'priority' = 0.5])
  ->removeCustomLinks()
  ->addCustomLink('/some/view/page', ['priority' = 0.5])
  ->generateSitemap();

Extending the module

It is possible to hook into link generation by implementing hook_simple_sitemap_links_alter(&$links){} in a custom module and altering the link array:

/**
 * 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.
 */
function hook_simple_sitemap_links_alter(&$links) {

  // Remove German URL for a certain path in the hreflang sitemap.
  foreach ($links as $key => $link) {
    if ($link['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){}. 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
 */
function hook_simple_sitemap_arbitrary_links_alter(array &$arbitrary_links) {

  // Add an arbitrary link.
  $arbitrary_links[] = [
    'url' => 'http://this-is-your-life.net/tyler',
    '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',
    ]
  ];
}

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

/**
 * Alters the sitemap attributes shortly before XML document generation.
 * Attributes can be added, changed and removed.
 *
 * @param array &$attributes
 */
function hook_simple_sitemap_attributes_alter(array &$attributes) {

  // Remove the xhtml attribute e.g. if no xhtml sitemap elements are present.
  unset($attributes['xmlns:xhtml']);
}
/**
 * Alters attributes of the sitemap index shortly before XML document generation.
 * Attributes can be added, changed and removed.
 *
 * @param array &$index_attributes
 */
function hook_simple_sitemap_index_attributes_alter(array &$index_attributes) {

  // 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 generator plugins.
 *
 * @param array $generators
 */
function hook_simple_sitemap_url_generators_alter(array &$generators) {

  // Remove the entity generator.
  // Useful when creating your own entity generator plugin.
  unset($generators['entity']);

  // Change the weight of the arbitrary link generator.
  $generators['arbitrary']['weight'] = -100;
}

In case this module's URL generators do not cover your use case, it is possible to implement new generator plugins in a custom module. To do it, simply extend the Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase class. See the generator plugins included in this module and check the API docs to learn how to implement plugins.