You are here

public function SitemapGeneratorBase::getIndexXml in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase::getIndexXml()

Return value

string

Throws

\Drupal\Core\Entity\EntityMalformedException

Overrides SitemapGeneratorInterface::getIndexXml

File

src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php, line 102

Class

SitemapGeneratorBase
Class SitemapGeneratorBase

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator

Code

public function getIndexXml() : string {
  $this->writer
    ->openMemory();
  $this->writer
    ->setIndent(TRUE);
  $this->writer
    ->startSitemapDocument();

  // Add the XML stylesheet to document if enabled.
  if ($this->settings
    ->get('xsl')) {
    $this->writer
      ->writeXsl();
  }
  $this->writer
    ->writeGeneratedBy();
  $this->writer
    ->startElement('sitemapindex');

  // Add attributes to document.
  $attributes = self::$indexAttributes;
  $sitemap_variant = $this->sitemapVariant;
  $this->moduleHandler
    ->alter('simple_sitemap_index_attributes', $attributes, $sitemap_variant);
  foreach ($attributes as $name => $value) {
    $this->writer
      ->writeAttribute($name, $value);
  }

  // Add sitemap chunk locations to document.
  for ($delta = 1; $delta <= $this->sitemapVariant
    ->fromUnpublished()
    ->getChunkCount(); $delta++) {
    $this->writer
      ->startElement('sitemap');
    $this->writer
      ->writeElement('loc', $this->sitemapVariant
      ->toUrl('canonical', [
      'delta' => $delta,
    ])
      ->toString());
    $this->writer
      ->writeElement('lastmod', date('c', $this->sitemapVariant
      ->fromUnpublished()
      ->getCreated()));

    // todo Should this be current time instead?
    $this->writer
      ->endElement();
  }
  $this->writer
    ->endElement();
  $this->writer
    ->endDocument();
  return $this->writer
    ->outputMemory();
}