class DefaultSitemapGenerator in Simple XML sitemap 4.x
Same name and namespace in other branches
- 8.3 src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\DefaultSitemapGenerator
Class DefaultSitemapGenerator
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 SimpleSitemapPluginInterface
- 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 SimpleSitemapPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DefaultSitemapGenerator
File
- src/
Plugin/ simple_sitemap/ SitemapGenerator/ DefaultSitemapGenerator.php, line 14
Namespace
Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGeneratorView source
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';
/**
* @var array
*/
protected const 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
*/
public function getChunkXml(array $links) : 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('urlset');
$this
->addSitemapAttributes();
$this
->addLinks($links);
$this->writer
->endElement();
$this->writer
->endDocument();
return $this->writer
->outputMemory();
}
/**
* Adds attributes to the sitemap.
*/
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);
}
}
/**
* Adds URL elements to the sitemap.
*
* @param array $links
*/
protected function addLinks(array $links) : void {
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) : void {
$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->sitemapVariant
->isMultilingual()) {
$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) : void {
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 string $language_id
* @param string $alternate_url
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | constant | ||
DefaultSitemapGenerator:: |
public | function |
Generates and returns a sitemap chunk. Overrides SitemapGeneratorBase:: |
|
DefaultSitemapGenerator:: |
protected | constant | ||
DefaultSitemapGenerator:: |
protected | constant | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SimpleSitemapPluginBase:: |
public | function |
Overrides SimpleSitemapPluginInterface:: |
|
SimpleSitemapPluginBase:: |
public | function |
Overrides SimpleSitemapPluginInterface:: |
|
SimpleSitemapPluginBase:: |
public | function |
Overrides SimpleSitemapPluginInterface:: |
|
SitemapGeneratorBase:: |
protected static | 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:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
public | function |
Overrides SitemapGeneratorInterface:: |
|
SitemapGeneratorBase:: |
protected | constant | ||
SitemapGeneratorBase:: |
public | function |
SitemapGeneratorBase constructor. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
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. |