diff --git a/simple_sitemap.routing.yml b/simple_sitemap.routing.yml index 7c1da48..c42c31a 100644 --- a/simple_sitemap.routing.yml +++ b/simple_sitemap.routing.yml @@ -26,6 +26,14 @@ simple_sitemap.chunk: requirements: _access: 'TRUE' +simple_sitemap.sitemap_xsl: + path: '/sitemap.xsl' + defaults: + _controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::renderSitemapXsl' + _title: 'Sitemap XSL' + requirements: + _access: 'TRUE' + simple_sitemap.settings: path: '/admin/config/search/simplesitemap' defaults: diff --git a/src/Controller/SimplesitemapController.php b/src/Controller/SimplesitemapController.php index 0e6d371..b6fa22a 100644 --- a/src/Controller/SimplesitemapController.php +++ b/src/Controller/SimplesitemapController.php @@ -78,4 +78,39 @@ class SimplesitemapController extends ControllerBase { return $response; } + + /** + * Provides the sitemap in XSL format. + * + * @return \Symfony\Component\HttpFoundation\Response + * Response object in XSL format. + */ + public function renderSitemapXsl() { + // 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'); + + // Make sure the strings in the XSL content are translated properly. + $replacements = [ + 'Sitemap file' => t('Sitemap file'), + 'Generated by the Drupal Simple XML sitemap module.' => t('Generated by the Drupal Simple XML sitemap module.', array('@link-simple_sitemap' => 'http://drupal.org/project/simple_sitemap')), + 'Number of sitemaps in this index' => t('Number of sitemaps in this index'), + 'Click on the table headers to change sorting.' => t('Click on the table headers to change sorting.'), + 'Sitemap URL' => t('Sitemap URL'), + 'Last modification date' => t('Last modification date'), + 'Number of URLs in this sitemap' => t('Number of URLs in this sitemap'), + 'URL location' => t('URL location'), + 'Translation set' => t('Translation set'), + 'Priority' => t('Priority'), + '[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/SitemapGenerator.php b/src/SitemapGenerator.php index aac9cb9..ddc412d 100644 --- a/src/SitemapGenerator.php +++ b/src/SitemapGenerator.php @@ -65,7 +65,7 @@ class SitemapGenerator { * @var array */ protected static $attributes = [ - 'xmlns' => self::XMLNS, + 'xmlns:sitemap' => self::XMLNS, 'xmlns:xhtml' => self::XMLNS_XHTML, 'xmlns:image' => self::XMLNS_IMAGE, ]; @@ -156,6 +156,7 @@ class SitemapGenerator { $this->writer->openMemory(); $this->writer->setIndent(TRUE); $this->writer->startDocument(self::XML_VERSION, self::ENCODING); + $this->writer->writePI('xml-stylesheet', 'type="text/xsl" href="' . $GLOBALS['base_url'] . '/sitemap.xsl"'); $this->writer->writeComment(self::GENERATED_BY); $this->writer->startElement('sitemapindex'); @@ -200,6 +201,7 @@ class SitemapGenerator { $this->writer->openMemory(); $this->writer->setIndent(TRUE); $this->writer->startDocument(self::XML_VERSION, self::ENCODING); + $this->writer->writePI('xml-stylesheet', 'type="text/xsl" href="' . $GLOBALS['base_url'] . '/sitemap.xsl"'); $this->writer->writeComment(self::GENERATED_BY); $this->writer->startElement('urlset');