EntityUrlGenerator.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
  3. use Drupal\simple_sitemap\EntityHelper;
  4. use Drupal\simple_sitemap\Logger;
  5. use Drupal\simple_sitemap\Simplesitemap;
  6. use Drupal\Core\Language\LanguageManagerInterface;
  7. use Drupal\Core\Entity\EntityTypeManagerInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Class EntityUrlGenerator
  11. * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
  12. *
  13. * @UrlGenerator(
  14. * id = "entity",
  15. * label = @Translation("Entity URL generator"),
  16. * description = @Translation("Generates URLs for entity bundles and bundle overrides."),
  17. * )
  18. */
  19. class EntityUrlGenerator extends EntityUrlGeneratorBase {
  20. /**
  21. * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
  22. */
  23. protected $urlGeneratorManager;
  24. /**
  25. * EntityUrlGenerator constructor.
  26. * @param array $configuration
  27. * @param $plugin_id
  28. * @param $plugin_definition
  29. * @param \Drupal\simple_sitemap\Simplesitemap $generator
  30. * @param \Drupal\simple_sitemap\Logger $logger
  31. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  32. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  33. * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
  34. * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
  35. */
  36. public function __construct(
  37. array $configuration,
  38. $plugin_id,
  39. $plugin_definition,
  40. Simplesitemap $generator,
  41. Logger $logger,
  42. LanguageManagerInterface $language_manager,
  43. EntityTypeManagerInterface $entity_type_manager,
  44. EntityHelper $entityHelper,
  45. UrlGeneratorManager $url_generator_manager
  46. ) {
  47. parent::__construct(
  48. $configuration,
  49. $plugin_id,
  50. $plugin_definition,
  51. $generator,
  52. $logger,
  53. $language_manager,
  54. $entity_type_manager,
  55. $entityHelper
  56. );
  57. $this->urlGeneratorManager = $url_generator_manager;
  58. }
  59. public static function create(
  60. ContainerInterface $container,
  61. array $configuration,
  62. $plugin_id,
  63. $plugin_definition) {
  64. return new static(
  65. $configuration,
  66. $plugin_id,
  67. $plugin_definition,
  68. $container->get('simple_sitemap.generator'),
  69. $container->get('simple_sitemap.logger'),
  70. $container->get('language_manager'),
  71. $container->get('entity_type.manager'),
  72. $container->get('simple_sitemap.entity_helper'),
  73. $container->get('plugin.manager.simple_sitemap.url_generator')
  74. );
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function getDataSets() {
  80. $data_sets = [];
  81. $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
  82. foreach ($this->generator->setVariants($this->sitemapVariant)->getBundleSettings() as $entity_type_name => $bundles) {
  83. if (isset($sitemap_entity_types[$entity_type_name])) {
  84. // Skip this entity type if another plugin is written to override its generation.
  85. foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
  86. if (isset($plugin['settings']['overrides_entity_type'])
  87. && $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
  88. continue 2;
  89. }
  90. }
  91. $entityTypeStorage = $this->entityTypeManager->getStorage($entity_type_name);
  92. $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
  93. foreach ($bundles as $bundle_name => $bundle_settings) {
  94. if (!empty($bundle_settings['index'])) {
  95. $query = $entityTypeStorage->getQuery();
  96. if (empty($keys['id'])) {
  97. $query->sort($keys['id'], 'ASC');
  98. }
  99. if (!empty($keys['bundle'])) {
  100. $query->condition($keys['bundle'], $bundle_name);
  101. }
  102. if (!empty($keys['status'])) {
  103. $query->condition($keys['status'], 1);
  104. }
  105. foreach ($query->execute() as $entity_id) {
  106. $data_sets[] = [
  107. 'entity_type' => $entity_type_name,
  108. 'id' => $entity_id,
  109. ];
  110. }
  111. }
  112. }
  113. }
  114. }
  115. return $data_sets;
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. protected function processDataSet($data_set) {
  121. if (empty($entity = $this->entityTypeManager->getStorage($data_set['entity_type'])->load($data_set['id']))) {
  122. return FALSE;
  123. }
  124. $entity_id = $entity->id();
  125. $entity_type_name = $entity->getEntityTypeId();
  126. $entity_settings = $this->generator
  127. ->setVariants($this->sitemapVariant)
  128. ->getEntityInstanceSettings($entity_type_name, $entity_id);
  129. if (empty($entity_settings['index'])) {
  130. return FALSE;
  131. }
  132. $url_object = $entity->toUrl();
  133. // Do not include external paths.
  134. if (!$url_object->isRouted()) {
  135. return FALSE;
  136. }
  137. $path = $url_object->getInternalPath();
  138. $url_object->setOption('absolute', TRUE);
  139. return [
  140. 'url' => $url_object,
  141. 'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
  142. 'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
  143. 'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
  144. 'images' => !empty($entity_settings['include_images'])
  145. ? $this->getImages($entity_type_name, $entity_id)
  146. : [],
  147. // Additional info useful in hooks.
  148. 'meta' => [
  149. 'path' => $path,
  150. 'entity_info' => [
  151. 'entity_type' => $entity_type_name,
  152. 'id' => $entity_id,
  153. ],
  154. ]
  155. ];
  156. }
  157. }