3 namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
5 use Drupal\simple_sitemap\EntityHelper;
6 use Drupal\simple_sitemap\Logger;
7 use Drupal\simple_sitemap\Simplesitemap;
8 use Drupal\simple_sitemap\SitemapGenerator;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Class EntityUrlGenerator
15 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
19 * title = @Translation("Entity URL generator"),
20 * description = @Translation("Generates URLs for entity bundles and bundle overrides."),
23 * "instantiate_for_each_data_set" = true,
27 class EntityUrlGenerator extends UrlGeneratorBase {
30 * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
32 protected $urlGeneratorManager;
35 * EntityMenuLinkContentUrlGenerator constructor.
36 * @param array $configuration
37 * @param string $plugin_id
38 * @param mixed $plugin_definition
39 * @param \Drupal\simple_sitemap\Simplesitemap $generator
40 * @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
41 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
42 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
43 * @param \Drupal\simple_sitemap\Logger $logger
44 * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
45 * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
47 public function __construct(
51 Simplesitemap $generator,
52 SitemapGenerator $sitemap_generator,
53 LanguageManagerInterface $language_manager,
54 EntityTypeManagerInterface $entity_type_manager,
56 EntityHelper $entityHelper,
57 UrlGeneratorManager $url_generator_manager
70 $this->urlGeneratorManager = $url_generator_manager;
73 public static function create(
74 ContainerInterface $container,
82 $container->get('simple_sitemap.generator'),
83 $container->get('simple_sitemap.sitemap_generator'),
84 $container->get('language_manager'),
85 $container->get('entity_type.manager'),
86 $container->get('simple_sitemap.logger'),
87 $container->get('simple_sitemap.entity_helper'),
88 $container->get('plugin.manager.simple_sitemap.url_generator')
95 public function getDataSets() {
97 $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
99 foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) {
100 if (isset($sitemap_entity_types[$entity_type_name])) {
102 // Skip this entity type if another plugin is written to override its generation.
103 foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
104 if ($plugin['enabled'] && !empty($plugin['settings']['overrides_entity_type'])
105 && $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
110 foreach ($bundles as $bundle_name => $bundle_settings) {
111 if ($bundle_settings['index']) {
113 'bundle_settings' => $bundle_settings,
114 'bundle_name' => $bundle_name,
115 'entity_type_name' => $entity_type_name,
116 'keys' => $sitemap_entity_types[$entity_type_name]->getKeys(),
129 protected function processDataSet($entity) {
131 $entity_id = $entity->id();
132 $entity_type_name = $entity->getEntityTypeId();
134 $entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
136 if (empty($entity_settings['index'])) {
140 $url_object = $entity->toUrl();
142 // Do not include external paths.
143 if (!$url_object->isRouted()) {
147 $path = $url_object->getInternalPath();
149 // Do not include paths that have been already indexed.
150 if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
154 $url_object->setOption('absolute', TRUE);
157 'url' => $url_object,
158 'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
159 'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
160 'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
161 'images' => !empty($entity_settings['include_images'])
162 ? $this->getImages($entity_type_name, $entity_id)
165 // Additional info useful in hooks.
169 'entity_type' => $entity_type_name,
179 protected function getBatchIterationElements($entity_info) {
180 $query = $this->entityTypeManager->getStorage($entity_info['entity_type_name'])->getQuery();
182 if (!empty($entity_info['keys']['id'])) {
183 $query->sort($entity_info['keys']['id'], 'ASC');
185 if (!empty($entity_info['keys']['bundle'])) {
186 $query->condition($entity_info['keys']['bundle'], $entity_info['bundle_name']);
188 if (!empty($entity_info['keys']['status'])) {
189 $query->condition($entity_info['keys']['status'], 1);
192 if ($this->needsInitialization()) {
193 $count_query = clone $query;
194 $this->initializeBatch($count_query->count()->execute());
197 if ($this->isBatch()) {
198 $query->range($this->context['sandbox']['progress'], $this->batchSettings['batch_process_limit']);
201 return $this->entityTypeManager
202 ->getStorage($entity_info['entity_type_name'])
203 ->loadMultiple($query->execute());