DefaultSitemapGenerator.php in Simple XML sitemap 8.3
File
src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
View source
<?php
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator;
class DefaultSitemapGenerator extends SitemapGeneratorBase {
const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
const XMLNS_IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
protected $isHreflangSitemap;
protected static $attributes = [
'xmlns' => self::XMLNS,
'xmlns:xhtml' => self::XMLNS_XHTML,
'xmlns:image' => self::XMLNS_IMAGE,
];
protected function getXml(array $links) {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startSitemapDocument();
if ($this->settings['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() {
$attributes = self::$attributes;
if (!$this
->isHreflangSitemap()) {
unset($attributes['xmlns:xhtml']);
}
$sitemap_variant = $this->sitemapVariant;
$this->moduleHandler
->alter('simple_sitemap_attributes', $attributes, $sitemap_variant);
foreach ($attributes as $name => $value) {
$this->writer
->writeAttribute($name, $value);
}
}
protected function addLinks(array $links) {
foreach ($links as $url_data) {
$this->writer
->startElement('url');
$this
->addUrl($url_data);
$this->writer
->endElement();
}
}
protected function addUrl(array $url_data) {
$this->writer
->writeElement('loc', $url_data['url']);
if (isset($url_data['alternate_urls']) && $this
->isHreflangSitemap()) {
$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) {
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($language_id, $alternate_url) {
$this->writer
->writeAttribute('rel', 'alternate');
$this->writer
->writeAttribute('hreflang', $language_id);
$this->writer
->writeAttribute('href', $alternate_url);
}
protected function isHreflangSitemap() {
return NULL !== $this->isHreflangSitemap ? $this->isHreflangSitemap : self::isMultilingualSitemap();
}
}
Classes
Name |
Description |
DefaultSitemapGenerator |
Class DefaultSitemapGenerator
@package Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator |