CustomUrlGenerator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
  3. use Drupal\Core\Url;
  4. use Drupal\simple_sitemap\Annotation\UrlGenerator;
  5. use Drupal\simple_sitemap\EntityHelper;
  6. use Drupal\simple_sitemap\Logger;
  7. use Drupal\simple_sitemap\Simplesitemap;
  8. use Drupal\Core\Language\LanguageManagerInterface;
  9. use Drupal\Core\Entity\EntityTypeManagerInterface;
  10. use Drupal\Core\Path\PathValidator;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Class CustomUrlGenerator
  14. * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
  15. *
  16. * @UrlGenerator(
  17. * id = "custom",
  18. * label = @Translation("Custom URL generator"),
  19. * description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
  20. * )
  21. *
  22. */
  23. class CustomUrlGenerator extends EntityUrlGeneratorBase {
  24. const PATH_DOES_NOT_EXIST_MESSAGE = 'The custom path @path has been omitted from the XML sitemaps as it does not exist. You can review custom paths <a href="@custom_paths_url">here</a>.';
  25. /**
  26. * @var \Drupal\Core\Path\PathValidator
  27. */
  28. protected $pathValidator;
  29. /**
  30. * @var bool
  31. */
  32. protected $includeImages;
  33. /**
  34. * CustomUrlGenerator constructor.
  35. * @param array $configuration
  36. * @param $plugin_id
  37. * @param $plugin_definition
  38. * @param \Drupal\simple_sitemap\Simplesitemap $generator
  39. * @param \Drupal\simple_sitemap\Logger $logger
  40. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  41. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  42. * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
  43. * @param \Drupal\Core\Path\PathValidator $path_validator
  44. */
  45. public function __construct(
  46. array $configuration,
  47. $plugin_id,
  48. $plugin_definition,
  49. Simplesitemap $generator,
  50. Logger $logger,
  51. LanguageManagerInterface $language_manager,
  52. EntityTypeManagerInterface $entity_type_manager,
  53. EntityHelper $entityHelper,
  54. PathValidator $path_validator) {
  55. parent::__construct(
  56. $configuration,
  57. $plugin_id,
  58. $plugin_definition,
  59. $generator,
  60. $logger,
  61. $language_manager,
  62. $entity_type_manager,
  63. $entityHelper
  64. );
  65. $this->pathValidator = $path_validator;
  66. }
  67. public static function create(
  68. ContainerInterface $container,
  69. array $configuration,
  70. $plugin_id,
  71. $plugin_definition) {
  72. return new static(
  73. $configuration,
  74. $plugin_id,
  75. $plugin_definition,
  76. $container->get('simple_sitemap.generator'),
  77. $container->get('simple_sitemap.logger'),
  78. $container->get('language_manager'),
  79. $container->get('entity_type.manager'),
  80. $container->get('simple_sitemap.entity_helper'),
  81. $container->get('path.validator')
  82. );
  83. }
  84. /**
  85. * @inheritdoc
  86. */
  87. public function getDataSets() {
  88. $this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
  89. return array_values($this->generator->setVariants($this->sitemapVariant)->getCustomLinks());
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. protected function processDataSet($data_set) {
  95. if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($data_set['path'])) {
  96. $this->logger->m(self::PATH_DOES_NOT_EXIST_MESSAGE,
  97. ['@path' => $data_set['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
  98. ->display('warning', 'administer sitemap settings')
  99. ->log('warning');
  100. return FALSE;
  101. }
  102. $url_object = Url::fromUserInput($data_set['path'], ['absolute' => TRUE]);
  103. $path = $url_object->getInternalPath();
  104. $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
  105. $path_data = [
  106. 'url' => $url_object,
  107. 'lastmod' => method_exists($entity, 'getChangedTime')
  108. ? date_iso8601($entity->getChangedTime()) : NULL,
  109. 'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
  110. 'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
  111. 'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId')
  112. ? $this->getImages($entity->getEntityTypeId(), $entity->id())
  113. : [],
  114. 'meta' => [
  115. 'path' => $path,
  116. ]
  117. ];
  118. // Additional info useful in hooks.
  119. if (NULL !== $entity) {
  120. $path_data['meta']['entity_info'] = [
  121. 'entity_type' => $entity->getEntityTypeId(),
  122. 'id' => $entity->id(),
  123. ];
  124. }
  125. return $path_data;
  126. }
  127. }