You are here

private function SitemapGenerator::generate_sitemap_chunk in Simple XML sitemap 8

Generates and returns a sitemap chunk.

Parameters

array $sitemap_links: All links with their translation and settings.

Return value

string sitemap chunk

1 call to SitemapGenerator::generate_sitemap_chunk()
SitemapGenerator::generate_sitemap in src/SitemapGenerator.php
Generates and returns the sitemap.

File

src/SitemapGenerator.php, line 128
Contains \Drupal\simplesitemap\SitemapGenerator.

Class

SitemapGenerator
SitemapGenerator class.

Namespace

Drupal\simplesitemap

Code

private function generate_sitemap_chunk($sitemap_links) {
  $writer = new XMLWriter();
  $writer
    ->openMemory();
  $writer
    ->setIndent(TRUE);
  $writer
    ->startDocument(self::XML_VERSION, self::ENCODING);
  $writer
    ->startElement('urlset');
  $writer
    ->writeAttribute('xmlns', self::XMLNS);
  $writer
    ->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
  foreach ($sitemap_links as $link) {
    $writer
      ->startElement('url');

    // Adding url to standard language.
    $writer
      ->writeElement('loc', $link['path_data']['urls'][$this->default_language_id]);

    // Adding alternate urls (other languages) if any.
    if (count($link['path_data']['urls']) > 1) {
      foreach ($link['path_data']['urls'] as $language_id => $localised_url) {
        $writer
          ->startElement('xhtml:link');
        $writer
          ->writeAttribute('rel', 'alternate');
        $writer
          ->writeAttribute('hreflang', $language_id);
        $writer
          ->writeAttribute('href', $localised_url);
        $writer
          ->endElement();
      }
    }

    // Add priority if any.
    if (!is_null($link['priority'])) {
      $writer
        ->writeElement('priority', $link['priority']);
    }

    // Add lastmod if any.
    if (!is_null($link['lastmod'])) {
      $writer
        ->writeElement('lastmod', $link['lastmod']);
    }
    $writer
      ->endElement();
  }
  $writer
    ->endElement();
  $writer
    ->endDocument();
  return $writer
    ->outputMemory();
}