DefaultSitemapGenerator.php in Simple XML sitemap 4.x
File
src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
View source
<?php
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator;
class DefaultSitemapGenerator extends SitemapGeneratorBase {
protected const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
protected const XMLNS_IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
protected const ATTRIBUTES = [
'xmlns' => self::XMLNS,
'xmlns:xhtml' => self::XMLNS_XHTML,
'xmlns:image' => self::XMLNS_IMAGE,
];
public function getChunkXml(array $links) : string {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startSitemapDocument();
if ($this->settings
->get('xsl')) {
$this->writer
->writeXsl();
}
$this->writer
->writeGeneratedBy();
$this->writer
->startElement('urlset');
$this
->addSitemapAttributes();
$this
->addLinks($links);
$this->writer
->endElement();
$this->writer
->endDocument();
return $this->writer
->outputMemory();
}
protected function addSitemapAttributes() : void {
$attributes = self::ATTRIBUTES;
if (!$this->sitemapVariant
->isMultilingual()) {
unset($attributes['xmlns:xhtml']);
}
$sitemap_variant = $this->sitemapVariant
->id();
$this->moduleHandler
->alter('simple_sitemap_attributes', $attributes, $sitemap_variant);
foreach ($attributes as $name => $value) {
$this->writer
->writeAttribute($name, $value);
}
}
protected function addLinks(array $links) : void {
foreach ($links as $url_data) {
$this->writer
->startElement('url');
$this
->addUrl($url_data);
$this->writer
->endElement();
}
}
protected function addUrl(array $url_data) : void {
$this->writer
->writeElement('loc', $url_data['url']);
if (isset($url_data['alternate_urls']) && $this->sitemapVariant
->isMultilingual()) {
$this
->addAlternateUrls($url_data['alternate_urls']);
}
if (isset($url_data['lastmod'])) {
$this->writer
->writeElement('lastmod', $url_data['lastmod']);
}
if (isset($url_data['changefreq'])) {
$this->writer
->writeElement('changefreq', $url_data['changefreq']);
}
if (isset($url_data['priority'])) {
$this->writer
->writeElement('priority', $url_data['priority']);
}
if (!empty($url_data['images'])) {
foreach ($url_data['images'] as $image) {
$this->writer
->startElement('image:image');
$this->writer
->writeElement('image:loc', $image['path']);
if (strlen($image['title']) > 0) {
$this->writer
->writeElement('image:title', $image['title']);
}
if (strlen($image['alt']) > 0) {
$this->writer
->writeElement('image:caption', $image['alt']);
}
$this->writer
->endElement();
}
}
}
protected function addAlternateUrls(array $alternate_urls) : void {
foreach ($alternate_urls as $language_id => $alternate_url) {
$this->writer
->startElement('xhtml:link');
$this
->addAlternateUrl($language_id, $alternate_url);
$this->writer
->endElement();
}
}
protected function addAlternateUrl(string $language_id, string $alternate_url) : void {
$this->writer
->writeAttribute('rel', 'alternate');
$this->writer
->writeAttribute('hreflang', $language_id);
$this->writer
->writeAttribute('href', $alternate_url);
}
}