diff --git a/modules/simple_sitemap_sitemapindex/README.md b/modules/simple_sitemap_sitemapindex/README.md new file mode 100644 index 0000000..3942536 --- /dev/null +++ b/modules/simple_sitemap_sitemapindex/README.md @@ -0,0 +1,20 @@ +# Simple XML Sitemap (SiteMapIndex) + +## What it do +Extend the module: 'Simple Sitemap' for generate a [Sitemap index](https://www.sitemaps.org/protocol.html#index) to group multiple sitemap files. +Example: https://www.euronics.it/sitemap.xml + +## Use +In the backend area: `/admin/config/search/simplesitemap/variants` add a new: `variants` with a new type: `sitemapindex` + +Example for a website with 2 sitemap and relative index: +```php +pages | default_hreflang | Pages +products | default_hreflang | Products +sitemapindex | sitemapindex | Sitemapindex +``` + +To use the `Sitemap index` as the default sitemap it is necessary to set the `Default sitemap variant` with the value: `Sitemapindex` in the advanced settings: + `/admin/config/search/simplesitemap/settings`. + +After hit the button: `Rebuild queue & generate` if everything worked correctly, the value: `published` will be displayed in the Status item of the line: `Sitemapindex` diff --git a/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.info.yml b/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.info.yml new file mode 100644 index 0000000..8e75dc2 --- /dev/null +++ b/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.info.yml @@ -0,0 +1,8 @@ +name: 'Simple XML Sitemap (Sitemap index)' +type: module +description: 'Extension for the Simple XML Sitemap module for generate Sitemap index' +configure: simple_sitemap.sitemaps +package: SEO +core_version_requirement: ^8 || ^9 +dependencies: + - simple_sitemap:simple_sitemap diff --git a/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.module b/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.module new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/modules/simple_sitemap_sitemapindex/simple_sitemap_sitemapindex.module @@ -0,0 +1 @@ +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('sitemapindex'); + + // Add attributes to document. + $attributes = self::$indexAttributes; + foreach ($attributes as $name => $value) { + $this->writer->writeAttribute($name, $value); + } + + foreach ($links as $link) { + $this->writer->startElement('sitemap'); + $this->writer->writeElement('loc', $link['loc']); + $this->writer->writeElement('lastmod', $link['lastmod']); + $this->writer->endElement(); + } + + $this->writer->endElement(); + $this->writer->endDocument(); + + return $this->writer->outputMemory(); + } + +} diff --git a/modules/simple_sitemap_sitemapindex/src/Plugin/simple_sitemap/SitemapType/SitemapindexType.php b/modules/simple_sitemap_sitemapindex/src/Plugin/simple_sitemap/SitemapType/SitemapindexType.php new file mode 100644 index 0000000..79410be --- /dev/null +++ b/modules/simple_sitemap_sitemapindex/src/Plugin/simple_sitemap/SitemapType/SitemapindexType.php @@ -0,0 +1,23 @@ +pathValidator = $path_validator; + $this->db = $database; + } + + /** + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * + * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase|\Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGeneratorBase|\Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase|static + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('simple_sitemap.generator'), + $container->get('simple_sitemap.logger'), + $container->get('language_manager'), + $container->get('entity_type.manager'), + $container->get('simple_sitemap.entity_helper'), + $container->get('path.validator'), + $container->get('database') + ); + } + + /** + * @return array|mixed + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + public function getDataSets() { + $data_set = []; + $sitemap_manager = $this->generator->getSitemapManager(); + $sitemap_settings = [ + 'base_url' => $this->generator->getSetting('base_url', ''), + 'default_variant' => $this->generator->getSetting('default_variant', NULL), + ]; + $sitemap_statuses = $this->fetchSitemapInstanceStatuses(); + + foreach ($this->generator->getSitemapManager()->getSitemapTypes() as $type_name => $type_definition) { + if (!empty($variants = $sitemap_manager->getSitemapVariants($type_name, FALSE))) { + $sitemap_generator = $sitemap_manager + ->getSitemapGenerator($type_definition['sitemapGenerator']) + ->setSettings($sitemap_settings); + foreach ($variants as $variant_name => $variant_definition) { + // Exclude sitemapindex. + if (isset($sitemap_statuses[$variant_name]) && $type_name != 'sitemapindex') { + switch ($sitemap_statuses[$variant_name]) { + case 1: + $data_set[$variant_name]['loc'] = $sitemap_generator->setSitemapVariant($variant_name)->getSitemapUrl(); + break; + } + } + } + } + } + return $data_set; + } + + /** + * @return array + * Array of sitemap statuses keyed by variant name. + * Status values: + * 0: Instance is unpublished + * 1: Instance is published + * 2: Instance is published but is being regenerated + * + * @todo Move to SitemapGeneratorBase or DefaultSitemapGenerator so it can be overwritten by sitemap types with custom storages. + */ + protected function fetchSitemapInstanceStatuses() { + $results = $this->db + ->query('SELECT type, status FROM {simple_sitemap} GROUP BY type, status') + ->fetchAll(); + + $instances = []; + foreach ($results as $i => $result) { + $instances[$result->type] = isset($instances[$result->type]) + ? $result->status + 1 + : (int) $result->status; + } + + return $instances; + } + + /** + * @param $data_set + * + * @return array|mixed + */ + protected function processDataSet($data_set) { + $path_data = [ + 'loc' => $data_set['loc'], + 'lastmod' => date('c'), + ]; + return $path_data; + } + +}