3 namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
6 use Drupal\simple_sitemap\Annotation\UrlGenerator;
7 use Drupal\simple_sitemap\EntityHelper;
8 use Drupal\simple_sitemap\Logger;
9 use Drupal\simple_sitemap\Simplesitemap;
10 use Drupal\simple_sitemap\SitemapGenerator;
11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Drupal\Core\Entity\EntityTypeManagerInterface;
13 use Drupal\Core\Path\PathValidator;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Class CustomUrlGenerator
18 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
22 * title = @Translation("Custom URL generator"),
23 * description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
28 class CustomUrlGenerator extends UrlGeneratorBase {
30 const PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE = 'The custom path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users. You can review custom paths <a href="@custom_paths_url">here</a>.';
34 * @var \Drupal\Core\Path\PathValidator
36 protected $pathValidator;
41 protected $includeImages;
44 * CustomUrlGenerator constructor.
45 * @param array $configuration
46 * @param string $plugin_id
47 * @param mixed $plugin_definition
48 * @param \Drupal\simple_sitemap\Simplesitemap $generator
49 * @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
50 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52 * @param \Drupal\simple_sitemap\Logger $logger
53 * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
54 * @param \Drupal\Core\Path\PathValidator $path_validator
56 public function __construct(
60 Simplesitemap $generator,
61 SitemapGenerator $sitemap_generator,
62 LanguageManagerInterface $language_manager,
63 EntityTypeManagerInterface $entity_type_manager,
65 EntityHelper $entityHelper,
66 PathValidator $path_validator) {
78 $this->pathValidator = $path_validator;
81 public static function create(
82 ContainerInterface $container,
90 $container->get('simple_sitemap.generator'),
91 $container->get('simple_sitemap.sitemap_generator'),
92 $container->get('language_manager'),
93 $container->get('entity_type.manager'),
94 $container->get('simple_sitemap.logger'),
95 $container->get('simple_sitemap.entity_helper'),
96 $container->get('path.validator')
103 public function getDataSets() {
104 $this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
106 return array_values($this->generator->getCustomLinks());
112 protected function processDataSet($data_set) {
114 // todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush. Use getUrlIfValidWithoutAccessCheck()?
115 if (!$this->pathValidator->isValid($data_set['path'])) {
116 // if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($data['path'])) {
117 $this->logger->m(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE,
118 ['@path' => $data_set['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
119 ->display('warning', 'administer sitemap settings')
124 $url_object = Url::fromUserInput($data_set['path'], ['absolute' => TRUE]);
125 $path = $url_object->getInternalPath();
127 if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
131 $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
134 'url' => $url_object,
135 'lastmod' => method_exists($entity, 'getChangedTime')
136 ? date_iso8601($entity->getChangedTime()) : NULL,
137 'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
138 'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
139 'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId')
140 ? $this->getImages($entity->getEntityTypeId(), $entity->id())
147 // Additional info useful in hooks.
148 if (NULL !== $entity) {
149 $path_data['meta']['entity_info'] = [
150 'entity_type' => $entity->getEntityTypeId(),
151 'id' => $entity->id(),