You are here

class DefaultSitemapGenerator in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of DefaultSitemapGenerator

File

src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php, line 14

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator
View 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

Namesort descending Modifiers Type Description Overrides
DefaultSitemapGenerator::addAlternateUrl protected function Adds a translation variant URL as alternate URL to a URL.
DefaultSitemapGenerator::addAlternateUrls protected function Adds all translation variant URLs as alternate URLs to a URL.
DefaultSitemapGenerator::addLinks protected function Adds URL elements to the sitemap.
DefaultSitemapGenerator::addSitemapAttributes protected function Adds attributes to the sitemap.
DefaultSitemapGenerator::addUrl protected function Adds a URL element to the sitemap.
DefaultSitemapGenerator::ATTRIBUTES protected constant
DefaultSitemapGenerator::getChunkXml public function Generates and returns a sitemap chunk. Overrides SitemapGeneratorBase::getChunkXml
DefaultSitemapGenerator::XMLNS_IMAGE protected constant
DefaultSitemapGenerator::XMLNS_XHTML protected constant
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SimpleSitemapPluginBase::description public function Overrides SimpleSitemapPluginInterface::description
SimpleSitemapPluginBase::label public function Overrides SimpleSitemapPluginInterface::label
SimpleSitemapPluginBase::settings public function Overrides SimpleSitemapPluginInterface::settings
SitemapGeneratorBase::$indexAttributes protected static property
SitemapGeneratorBase::$moduleHandler protected property
SitemapGeneratorBase::$settings protected property
SitemapGeneratorBase::$sitemapVariant protected property
SitemapGeneratorBase::$writer protected property
SitemapGeneratorBase::create public static function Creates an instance of the plugin. Overrides SimpleSitemapPluginBase::create
SitemapGeneratorBase::getIndexXml public function Overrides SitemapGeneratorInterface::getIndexXml
SitemapGeneratorBase::setSitemapVariant public function Overrides SitemapGeneratorInterface::setSitemapVariant
SitemapGeneratorBase::XMLNS protected constant
SitemapGeneratorBase::__construct public function SitemapGeneratorBase constructor. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.