diff --git a/composer.json b/composer.json index f8c19b9..ec59049 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,23 @@ }, "license": "GPL-2.0+", "minimum-stability": "dev", + "repositories": [ + { + "type": "package", + "package": { + "name": "mottie/tablesorter", + "version": "master", + "type": "drupal-library", + "dist": { + "url": "https://github.com/Mottie/tablesorter/archive/master.zip", + "type": "zip" + } + } + } + ], "require": { - "ext-xmlwriter": "*" + "ext-xmlwriter": "*", + "mottie/tablesorter": "master" }, "extra": { "drush": { @@ -29,4 +44,3 @@ } } } - diff --git a/config/install/simple_sitemap.settings.yml b/config/install/simple_sitemap.settings.yml index 1d4c8e0..bf7fac1 100644 --- a/config/install/simple_sitemap.settings.yml +++ b/config/install/simple_sitemap.settings.yml @@ -4,6 +4,7 @@ cron_generate_interval: 0 generate_duration: 10000 remove_duplicates: true skip_untranslated: true +xsl: true base_url: '' default_variant: 'default' custom_links_include_images: false diff --git a/config/schema/simple_sitemap.schema.yml b/config/schema/simple_sitemap.schema.yml index 9fd7712..2acc8c1 100644 --- a/config/schema/simple_sitemap.schema.yml +++ b/config/schema/simple_sitemap.schema.yml @@ -19,6 +19,9 @@ simple_sitemap.settings: skip_untranslated: label: 'Skip untranslated' type: boolean + xsl: + label: 'Include a stylesheet in the sitemaps for humans' + type: boolean base_url: label: 'Base URL' type: string diff --git a/simple_sitemap.routing.yml b/simple_sitemap.routing.yml index eaa9fbd..2eda68e 100644 --- a/simple_sitemap.routing.yml +++ b/simple_sitemap.routing.yml @@ -49,3 +49,11 @@ simple_sitemap.settings_variants: _title: 'Simple XML Sitemap Settings' requirements: _permission: 'administer sitemap settings' + +simple_sitemap.sitemap_xsl: + path: '/sitemap.xsl' + defaults: + _controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::getSitemapXsl' + _title: 'Sitemap XSL' + requirements: + _access: 'TRUE' diff --git a/src/Controller/SimplesitemapController.php b/src/Controller/SimplesitemapController.php index b8c637e..48ec388 100644 --- a/src/Controller/SimplesitemapController.php +++ b/src/Controller/SimplesitemapController.php @@ -67,4 +67,40 @@ class SimplesitemapController extends ControllerBase { 'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself. ]); } + + /** + * Returns the XML stylesheet for sitemap. + */ + public function getSitemapXsl() { + // Read the XSL content from the file. + $module_path = drupal_get_path('module', 'simple_sitemap'); + $xsl_content = file_get_contents($module_path . '/xsl/simple_sitemap.xsl'); + + // Replace custom tokens in the XSL content with appropriate values. + $replacements = [ + '[title]' => $this->t('Sitemap file'), + '[generated-by]' => $this->t('Generated by the Drupal Simple XML sitemap module.', ['@link' => 'https://www.drupal.org/project/simple_sitemap']), + '[number-of-sitemaps]' => $this->t('Number of sitemaps in this index'), + '[sitemap-url]' => $this->t('Sitemap URL'), + '[number-of-urls]' => $this->t('Number of URLs in this sitemap'), + '[url-location]' => $this->t('URL location'), + '[lastmod]' => $this->t('Last modification date'), + '[changefreq]' => $this->t('Change frequency'), + '[priority]' => $this->t('Priority'), + '[translation-set]' => $this->t('Translation set'), + '[images]' => $this->t('Images'), + '[jquery]' => base_path() . 'core/assets/vendor/jquery/jquery.min.js', + '[jquery-tablesorter]' => base_path() . 'libraries/tablesorter/dist/js/jquery.tablesorter.min.js', + '[xsl-js]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.js', + '[xsl-css]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.css', + ]; + $xsl_content = strtr($xsl_content, $replacements); + + // Output the XSL content. + $response = new Response($xsl_content); + $response->headers->set('Content-type', 'application/xml; charset=utf-8'); + $response->headers->set('X-Robots-Tag', 'noindex, follow'); + return $response; + } + } diff --git a/src/Form/SimplesitemapSettingsForm.php b/src/Form/SimplesitemapSettingsForm.php index 92835b3..6e511cd 100644 --- a/src/Form/SimplesitemapSettingsForm.php +++ b/src/Form/SimplesitemapSettingsForm.php @@ -237,6 +237,13 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ], ]; + $form['simple_sitemap_settings']['settings']['xsl'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Include a stylesheet in the sitemaps for humans'), + '#description' => $this->t('When enabled, this will add formatting and tables with sorting to make it easier to view the XML sitemap data instead of viewing raw XML output. Search engines will ignore this.'), + '#default_value' => $this->generator->getSetting('xsl', TRUE), + ]; + $form['simple_sitemap_settings']['settings']['languages'] = [ '#type' => 'details', '#title' => $this->t('Language settings'), @@ -363,6 +370,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { 'cron_generate_interval', 'remove_duplicates', 'skip_untranslated', + 'xsl', 'base_url', 'default_variant'] as $setting_name) { $this->generator->saveSetting($setting_name, $form_state->getValue($setting_name)); diff --git a/src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php b/src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php index 71f40ef..1aebdd9 100644 --- a/src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php +++ b/src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php @@ -100,6 +100,10 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase { $this->writer->openMemory(); $this->writer->setIndent(TRUE); $this->writer->startDocument(self::XML_VERSION, self::ENCODING); + // Add the XML stylesheet to document if necessary. + if ($this->settings['xsl']) { + $this->writer->writeXsl(); + } $this->writer->writeComment(self::GENERATED_BY); $this->writer->startElement('urlset'); diff --git a/src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php b/src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php index 9524534..7037169 100644 --- a/src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php +++ b/src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php @@ -148,6 +148,10 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S $this->writer->openMemory(); $this->writer->setIndent(TRUE); $this->writer->startDocument(self::XML_VERSION, self::ENCODING); + // Add the XML stylesheet to document if necessary. + if ($this->settings['xsl']) { + $this->writer->writeXsl(); + } $this->writer->writeComment(self::GENERATED_BY); $this->writer->startElement('sitemapindex'); @@ -300,4 +304,5 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S $customBaseUrl = $this->settings['base_url']; return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url']; } + } diff --git a/src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php b/src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php index 7085162..ea9002e 100644 --- a/src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php +++ b/src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php @@ -2,12 +2,20 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator; -use XMLWriter; +use Drupal\Core\Url; /** * Class SitemapWriter * @package Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator */ -class SitemapWriter extends XMLWriter { +class SitemapWriter extends \XMLWriter { + + /** + * Adds the XML stylesheet to the XML page. + */ + public function writeXsl() { + $xsl_url = Url::fromRoute('simple_sitemap.sitemap_xsl')->toString(); + $this->writePI('xml-stylesheet', 'type="text/xsl" href="' . $xsl_url . '"'); + } } diff --git a/src/Queue/QueueWorker.php b/src/Queue/QueueWorker.php index ee7c59c..daf03da 100644 --- a/src/Queue/QueueWorker.php +++ b/src/Queue/QueueWorker.php @@ -193,6 +193,7 @@ class QueueWorker { $this->generatorSettings = [ 'base_url' => $this->settings->getSetting('base_url', ''), + 'xsl' => $this->settings->getSetting('xsl', TRUE), 'default_variant' => $this->settings->getSetting('default_variant', NULL), 'skip_untranslated' => $this->settings->getSetting('skip_untranslated', FALSE), 'remove_duplicates' => $this->settings->getSetting('remove_duplicates', TRUE), diff --git a/xsl/simple_sitemap.xsl b/xsl/simple_sitemap.xsl new file mode 100644 index 0000000..833a531 --- /dev/null +++ b/xsl/simple_sitemap.xsl @@ -0,0 +1,184 @@ + + + + + + + + + [title] +