class DefaultSitemapGenerator in Simple XML sitemap 8.3
Same name and namespace in other branches
- 4.x src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\DefaultSitemapGenerator
Class DefaultSitemapGenerator @package Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator
Plugin annotation
@SitemapGenerator(
id = "default",
label = @Translation("Default sitemap generator"),
description = @Translation("Generates a standard conform hreflang sitemap of your content."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase implements PluginInspectionInterface, ContainerFactoryPluginInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase implements SitemapGeneratorInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\DefaultSitemapGenerator
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase implements SitemapGeneratorInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase implements PluginInspectionInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DefaultSitemapGenerator
File
- src/
Plugin/ simple_sitemap/ SitemapGenerator/ DefaultSitemapGenerator.php, line 15
Namespace
Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGeneratorView source
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';
/**
* @var bool
*/
protected $isHreflangSitemap;
/**
* @var array
*/
protected static $attributes = [
'xmlns' => self::XMLNS,
'xmlns:xhtml' => self::XMLNS_XHTML,
'xmlns:image' => self::XMLNS_IMAGE,
];
/**
* Generates and returns a sitemap chunk.
*
* @param array $links
* All links with their multilingual versions and settings.
*
* @return string
* Sitemap chunk
*/
protected function getXml(array $links) {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startSitemapDocument();
// Add the XML stylesheet to document if enabled.
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();
}
/**
* Adds attributes to the sitemap.
*/
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);
}
}
/**
* Adds URL elements to the sitemap.
*
* @param array $links
*/
protected function addLinks(array $links) {
foreach ($links as $url_data) {
$this->writer
->startElement('url');
$this
->addUrl($url_data);
$this->writer
->endElement();
}
}
/**
* Adds a URL element to the sitemap.
*
* @param array $url_data
* The array of properties for this URL.
*/
protected function addUrl(array $url_data) {
$this->writer
->writeElement('loc', $url_data['url']);
// If more than one language is enabled, add all translation variant URLs
// as alternate links to this link turning the sitemap into a hreflang
// sitemap.
if (isset($url_data['alternate_urls']) && $this
->isHreflangSitemap()) {
$this
->addAlternateUrls($url_data['alternate_urls']);
}
// Add lastmod if any.
if (isset($url_data['lastmod'])) {
$this->writer
->writeElement('lastmod', $url_data['lastmod']);
}
// Add changefreq if any.
if (isset($url_data['changefreq'])) {
$this->writer
->writeElement('changefreq', $url_data['changefreq']);
}
// Add priority if any.
if (isset($url_data['priority'])) {
$this->writer
->writeElement('priority', $url_data['priority']);
}
// Add images if any.
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();
}
}
}
/**
* Adds all translation variant URLs as alternate URLs to a URL.
*
* @param array $alternate_urls
*/
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();
}
}
/**
* Adds a translation variant URL as alternate URL to a URL.
*
* @param $language_id
* @param $alternate_url
*/
protected function addAlternateUrl($language_id, $alternate_url) {
$this->writer
->writeAttribute('rel', 'alternate');
$this->writer
->writeAttribute('hreflang', $language_id);
$this->writer
->writeAttribute('href', $alternate_url);
}
/**
* Checks if sitemap is hreflang compliant.
*
* @return bool
*/
protected function isHreflangSitemap() {
return NULL !== $this->isHreflangSitemap ? $this->isHreflangSitemap : self::isMultilingualSitemap();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultSitemapGenerator:: |
protected static | property | ||
DefaultSitemapGenerator:: |
protected | property | ||
DefaultSitemapGenerator:: |
protected | function | Adds a translation variant URL as alternate URL to a URL. | |
DefaultSitemapGenerator:: |
protected | function | Adds all translation variant URLs as alternate URLs to a URL. | |
DefaultSitemapGenerator:: |
protected | function | Adds URL elements to the sitemap. | |
DefaultSitemapGenerator:: |
protected | function | Adds attributes to the sitemap. | |
DefaultSitemapGenerator:: |
protected | function | Adds a URL element to the sitemap. | |
DefaultSitemapGenerator:: |
protected | function |
Generates and returns a sitemap chunk. Overrides SitemapGeneratorBase:: |
|
DefaultSitemapGenerator:: |
protected | function | Checks if sitemap is hreflang compliant. | |
DefaultSitemapGenerator:: |
constant | |||
DefaultSitemapGenerator:: |
constant | |||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected static | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
protected | property | ||
SitemapGeneratorBase:: |
public static | function |
Creates an instance of the plugin. Overrides SimplesitemapPluginBase:: |
|
SitemapGeneratorBase:: |
constant | |||
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
protected | function | ||
SitemapGeneratorBase:: |
protected | function | ||
SitemapGeneratorBase:: |
protected | function | ||
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
protected | function | ||
SitemapGeneratorBase:: |
constant | |||
SitemapGeneratorBase:: |
protected | function | ||
SitemapGeneratorBase:: |
public static | function | Determines if the sitemap is to be a multilingual sitemap based on several factors. | |
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
public static | function | ||
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
constant | |||
SitemapGeneratorBase:: |
public | function |
SitemapGeneratorBase constructor. Overrides SimplesitemapPluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |