You are here

abstract class SitemapGeneratorBase in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase

Class SitemapGeneratorBase

Hierarchy

Expanded class hierarchy of SitemapGeneratorBase

File

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

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator
View source
abstract class SitemapGeneratorBase extends SimpleSitemapPluginBase implements SitemapGeneratorInterface {
  protected const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';

  /**
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * @var \Drupal\simple_sitemap\Settings
   */
  protected $settings;

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter
   */
  protected $writer;

  /**
   * @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface
   */
  protected $sitemapVariant;

  /**
   * @var array
   */
  protected static $indexAttributes = [
    'xmlns' => self::XMLNS,
  ];

  /**
   * SitemapGeneratorBase constructor.
   *
   * @param array $configuration
   * @param $plugin_id
   * @param $plugin_definition
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter $sitemap_writer
   * @param \Drupal\simple_sitemap\Settings $settings
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandler $module_handler, SitemapWriter $sitemap_writer, Settings $settings) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->moduleHandler = $module_handler;
    $this->writer = $sitemap_writer;
    $this->settings = $settings;
  }
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : SimpleSitemapPluginBase {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('module_handler'), $container
      ->get('simple_sitemap.sitemap_writer'), $container
      ->get('simple_sitemap.settings'));
  }

  /**
   * @param \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap
   *
   * @return $this
   */
  public function setSitemapVariant(SimpleSitemapInterface $sitemap) : SitemapGeneratorInterface {
    $this->sitemapVariant = $sitemap;
    return $this;
  }

  /**
   * @param array $links
   * @return string
   */
  public abstract function getChunkXml(array $links) : string;

  /**
   * @return string
   *
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function getIndexXml() : 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('sitemapindex');

    // Add attributes to document.
    $attributes = self::$indexAttributes;
    $sitemap_variant = $this->sitemapVariant;
    $this->moduleHandler
      ->alter('simple_sitemap_index_attributes', $attributes, $sitemap_variant);
    foreach ($attributes as $name => $value) {
      $this->writer
        ->writeAttribute($name, $value);
    }

    // Add sitemap chunk locations to document.
    for ($delta = 1; $delta <= $this->sitemapVariant
      ->fromUnpublished()
      ->getChunkCount(); $delta++) {
      $this->writer
        ->startElement('sitemap');
      $this->writer
        ->writeElement('loc', $this->sitemapVariant
        ->toUrl('canonical', [
        'delta' => $delta,
      ])
        ->toString());
      $this->writer
        ->writeElement('lastmod', date('c', $this->sitemapVariant
        ->fromUnpublished()
        ->getCreated()));

      // todo Should this be current time instead?
      $this->writer
        ->endElement();
    }
    $this->writer
      ->endElement();
    $this->writer
      ->endDocument();
    return $this->writer
      ->outputMemory();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::getChunkXml abstract public function Overrides SitemapGeneratorInterface::getChunkXml 1
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.