DefaultSitemapGenerator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Drupal\Core\Database\Connection;
  5. use Drupal\Core\Extension\ModuleHandler;
  6. use Drupal\Core\Language\LanguageManagerInterface;
  7. use Drupal\Component\Datetime\Time;
  8. /**
  9. * Class DefaultSitemapGenerator
  10. * @package Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator
  11. *
  12. * @SitemapGenerator(
  13. * id = "default",
  14. * label = @Translation("Default sitemap generator"),
  15. * description = @Translation("Generates a standard conform hreflang sitemap of your content."),
  16. * )
  17. */
  18. class DefaultSitemapGenerator extends SitemapGeneratorBase {
  19. const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
  20. const XMLNS_IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
  21. /**
  22. * @var bool
  23. */
  24. protected $isHreflangSitemap;
  25. /**
  26. * @var array
  27. */
  28. protected static $attributes = [
  29. 'xmlns' => self::XMLNS,
  30. 'xmlns:xhtml' => self::XMLNS_XHTML,
  31. 'xmlns:image' => self::XMLNS_IMAGE,
  32. ];
  33. /**
  34. * DefaultSitemapGenerator constructor.
  35. * @param array $configuration
  36. * @param string $plugin_id
  37. * @param mixed $plugin_definition
  38. * @param \Drupal\Core\Database\Connection $database
  39. * @param \Drupal\Core\Extension\ModuleHandler $module_handler
  40. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  41. * @param \Drupal\Component\Datetime\Time $time
  42. * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter $sitemapWriter
  43. */
  44. public function __construct(
  45. array $configuration,
  46. $plugin_id,
  47. $plugin_definition,
  48. Connection $database,
  49. ModuleHandler $module_handler,
  50. LanguageManagerInterface $language_manager,
  51. Time $time,
  52. SitemapWriter $sitemapWriter
  53. ) {
  54. parent::__construct(
  55. $configuration,
  56. $plugin_id,
  57. $plugin_definition,
  58. $database,
  59. $module_handler,
  60. $language_manager,
  61. $time,
  62. $sitemapWriter
  63. );
  64. }
  65. public static function create(
  66. ContainerInterface $container,
  67. array $configuration,
  68. $plugin_id,
  69. $plugin_definition) {
  70. return new static(
  71. $configuration,
  72. $plugin_id,
  73. $plugin_definition,
  74. $container->get('database'),
  75. $container->get('module_handler'),
  76. $container->get('language_manager'),
  77. $container->get('datetime.time'),
  78. $container->get('simple_sitemap.sitemap_writer')
  79. );
  80. }
  81. /**
  82. * Generates and returns a sitemap chunk.
  83. *
  84. * @param array $links
  85. * All links with their multilingual versions and settings.
  86. *
  87. * @return string
  88. * Sitemap chunk
  89. */
  90. protected function getXml(array $links) {
  91. $this->writer->openMemory();
  92. $this->writer->setIndent(TRUE);
  93. $this->writer->startDocument(self::XML_VERSION, self::ENCODING);
  94. $this->writer->writeComment(self::GENERATED_BY);
  95. $this->writer->startElement('urlset');
  96. // Add attributes to document.
  97. $attributes = self::$attributes;
  98. if (!$this->isHreflangSitemap()) {
  99. unset($attributes['xmlns:xhtml']);
  100. }
  101. $sitemap_variant = $this->sitemapVariant;
  102. $this->moduleHandler->alter('simple_sitemap_attributes', $attributes, $sitemap_variant);
  103. foreach ($attributes as $name => $value) {
  104. $this->writer->writeAttribute($name, $value);
  105. }
  106. // Add URLs to document.
  107. $sitemap_variant = $this->sitemapVariant;
  108. $this->moduleHandler->alter('simple_sitemap_links', $links, $sitemap_variant);
  109. foreach ($links as $link) {
  110. // Add each translation variant URL as location to the sitemap.
  111. $this->writer->startElement('url');
  112. $this->writer->writeElement('loc', $link['url']);
  113. // If more than one language is enabled, add all translation variant URLs
  114. // as alternate links to this location turning the sitemap into a hreflang
  115. // sitemap.
  116. if (isset($link['alternate_urls']) && $this->isHreflangSitemap()) {
  117. foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
  118. $this->writer->startElement('xhtml:link');
  119. $this->writer->writeAttribute('rel', 'alternate');
  120. $this->writer->writeAttribute('hreflang', $language_id);
  121. $this->writer->writeAttribute('href', $alternate_url);
  122. $this->writer->endElement();
  123. }
  124. }
  125. // Add lastmod if any.
  126. if (isset($link['lastmod'])) {
  127. $this->writer->writeElement('lastmod', $link['lastmod']);
  128. }
  129. // Add changefreq if any.
  130. if (isset($link['changefreq'])) {
  131. $this->writer->writeElement('changefreq', $link['changefreq']);
  132. }
  133. // Add priority if any.
  134. if (isset($link['priority'])) {
  135. $this->writer->writeElement('priority', $link['priority']);
  136. }
  137. // Add images if any.
  138. if (!empty($link['images'])) {
  139. foreach ($link['images'] as $image) {
  140. $this->writer->startElement('image:image');
  141. $this->writer->writeElement('image:loc', $image['path']);
  142. $this->writer->endElement();
  143. }
  144. }
  145. $this->writer->endElement();
  146. }
  147. $this->writer->endElement();
  148. $this->writer->endDocument();
  149. return $this->writer->outputMemory();
  150. }
  151. /**
  152. * @return bool
  153. */
  154. protected function isHreflangSitemap() {
  155. if (NULL === $this->isHreflangSitemap) {
  156. $this->isHreflangSitemap = count(
  157. array_diff_key($this->languageManager->getLanguages(),
  158. $this->settings['excluded_languages'])
  159. ) > 1;
  160. }
  161. return $this->isHreflangSitemap;
  162. }
  163. }