1b36dd3d51cc46dbfe016f60ce85ee8bd5823c12
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Plugin / simple_sitemap / UrlGenerator / CustomUrlGenerator.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
4
5 use Drupal\Core\Url;
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;
15
16 /**
17  * Class CustomUrlGenerator
18  * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
19  *
20  * @UrlGenerator(
21  *   id = "custom",
22  *   title = @Translation("Custom URL generator"),
23  *   description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
24  *   weight = 0,
25  * )
26  *
27  */
28 class CustomUrlGenerator extends UrlGeneratorBase {
29
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>.';
31
32
33   /**
34    * @var \Drupal\Core\Path\PathValidator
35    */
36   protected $pathValidator;
37
38   /**
39    * @var bool
40    */
41   protected $includeImages;
42
43   /**
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
55    */
56   public function __construct(
57     array $configuration,
58     $plugin_id,
59     $plugin_definition,
60     Simplesitemap $generator,
61     SitemapGenerator $sitemap_generator,
62     LanguageManagerInterface $language_manager,
63     EntityTypeManagerInterface $entity_type_manager,
64     Logger $logger,
65     EntityHelper $entityHelper,
66     PathValidator $path_validator) {
67     parent::__construct(
68       $configuration,
69       $plugin_id,
70       $plugin_definition,
71       $generator,
72       $sitemap_generator,
73       $language_manager,
74       $entity_type_manager,
75       $logger,
76       $entityHelper
77     );
78     $this->pathValidator = $path_validator;
79   }
80
81   public static function create(
82     ContainerInterface $container,
83     array $configuration,
84     $plugin_id,
85     $plugin_definition) {
86     return new static(
87       $configuration,
88       $plugin_id,
89       $plugin_definition,
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')
97     );
98   }
99
100   /**
101    * @inheritdoc
102    */
103   public function getDataSets() {
104     $this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
105
106     return array_values($this->generator->getCustomLinks());
107   }
108
109   /**
110    * @inheritdoc
111    */
112   protected function processDataSet($data_set) {
113
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')
120           ->log('warning');
121         return FALSE;
122       }
123
124       $url_object = Url::fromUserInput($data_set['path'], ['absolute' => TRUE]);
125       $path = $url_object->getInternalPath();
126
127       if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
128         return FALSE;
129       }
130
131       $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
132
133       $path_data = [
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())
141           : [],
142         'meta' => [
143           'path' => $path,
144         ]
145       ];
146
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(),
152         ];
153       }
154
155       return $path_data;
156   }
157 }