You are here

function xmlsitemap_sitemap_save in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \xmlsitemap_sitemap_save()
  2. 6.2 xmlsitemap.module \xmlsitemap_sitemap_save()
  3. 7.2 xmlsitemap.module \xmlsitemap_sitemap_save()

Save changes to an XML sitemap or add a new XML sitemap.

@todo Save the sitemap's URL as a column?

Parameters

Drupal\xmlsitemap\XmlSitemapInterface $sitemap: The XML sitemap array to be saved. If $sitemap->smid is omitted, a new XML sitemap will be added.

Related topics

2 calls to xmlsitemap_sitemap_save()
XmlSitemapGenerator::regenerateBatchGenerate in src/XmlSitemapGenerator.php
Batch callback; generate all pages of a sitemap.
XmlSitemapMultilingualTestBase::setUp in tests/src/Functional/XmlSitemapMultilingualTestBase.php
Set up an administrative user account and testing keys.

File

./xmlsitemap.module, line 349
xmlsitemap XML sitemap

Code

function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
  $context = $sitemap->context;
  if (!isset($context) || !$context) {
    $sitemap->context = [];
  }

  // Make sure context is sorted before saving the hash.
  $sitemap
    ->setOriginalId($sitemap
    ->isNew() ? NULL : $sitemap
    ->getId());
  $sitemap
    ->setId(xmlsitemap_sitemap_get_context_hash($context));

  // If the context was changed, we need to perform additional actions.
  if (!$sitemap
    ->isNew() && $sitemap
    ->getId() != $sitemap
    ->getOriginalId()) {

    // Rename the files directory so the sitemap does not break.
    $old_sitemap = xmlsitemap_sitemap_load($sitemap
      ->getOriginalId());
    $old_dir = xmlsitemap_get_directory($old_sitemap);
    $new_dir = xmlsitemap_get_directory($sitemap);
    xmlsitemap_directory_move($old_dir, $new_dir);

    // Mark the sitemaps as needing regeneration.
    \Drupal::state()
      ->set('xmlsitemap_regenerate_needed', TRUE);
  }
  $sitemap
    ->save();
  return $sitemap;
}