You are here

public function XmlSitemapGenerator::generateIndex in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/XmlSitemapGenerator.php \Drupal\xmlsitemap\XmlSitemapGenerator::generateIndex()

Generate the index sitemap.

Parameters

\Drupal\xmlsitemap\XmlSitemapInterface $sitemap: The XML sitemap config entity.

int|null $pages: The number of pages to write in the sitemap. Defaults to the value of $sitemap->getChunks().

Overrides XmlSitemapGeneratorInterface::generateIndex

1 call to XmlSitemapGenerator::generateIndex()
XmlSitemapGenerator::regenerateBatchGenerateIndex in src/XmlSitemapGenerator.php
Batch callback; generate the index page of a sitemap.

File

src/XmlSitemapGenerator.php, line 361

Class

XmlSitemapGenerator
XmlSitemap generator service class.

Namespace

Drupal\xmlsitemap

Code

public function generateIndex(XmlSitemapInterface $sitemap, $pages = NULL) {
  $writer = new XmlSitemapWriter($sitemap, 'index');
  $writer
    ->startDocument();
  $lastmod_format = $this->config
    ->get('lastmod_format');
  $url_options = $sitemap->uri['options'];
  $url_options += [
    'absolute' => TRUE,
    'xmlsitemap_base_url' => $this->state
      ->get('xmlsitemap_base_url'),
    'language' => $this->languageManager
      ->getDefaultLanguage(),
    'alias' => TRUE,
  ];
  if (!isset($pages)) {
    $pages = $sitemap
      ->getChunks();
  }
  for ($current_page = 1; $current_page <= $pages; $current_page++) {
    $url_options['query']['page'] = $current_page;
    $element = [
      'loc' => Url::fromRoute('xmlsitemap.sitemap_xml', [], $url_options)
        ->toString(),
      // @todo Use the actual lastmod value of the chunk file.
      'lastmod' => gmdate($lastmod_format, $this->time
        ->getRequestTime()),
    ];

    // @todo Should the element be altered?
    $writer
      ->writeElement('sitemap', $element);
  }
  $writer
    ->endDocument();
  return $writer
    ->getSitemapElementCount();
}