Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Plugin / simple_sitemap / UrlGenerator / EntityUrlGenerator.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
4
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;
12
13 /**
14  * Class EntityUrlGenerator
15  * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
16  *
17  * @UrlGenerator(
18  *   id = "entity",
19  *   title = @Translation("Entity URL generator"),
20  *   description = @Translation("Generates URLs for entity bundles and bundle overrides."),
21  *   weight = 10,
22  *   settings = {
23  *     "instantiate_for_each_data_set" = true,
24  *   },
25  * )
26  */
27 class EntityUrlGenerator extends UrlGeneratorBase {
28
29   /**
30    * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
31    */
32   protected $urlGeneratorManager;
33
34   /**
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
46    */
47   public function __construct(
48     array $configuration,
49     $plugin_id,
50     $plugin_definition,
51     Simplesitemap $generator,
52     SitemapGenerator $sitemap_generator,
53     LanguageManagerInterface $language_manager,
54     EntityTypeManagerInterface $entity_type_manager,
55     Logger $logger,
56     EntityHelper $entityHelper,
57     UrlGeneratorManager $url_generator_manager
58   ) {
59     parent::__construct(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $generator,
64       $sitemap_generator,
65       $language_manager,
66       $entity_type_manager,
67       $logger,
68       $entityHelper
69     );
70     $this->urlGeneratorManager = $url_generator_manager;
71   }
72
73   public static function create(
74     ContainerInterface $container,
75     array $configuration,
76     $plugin_id,
77     $plugin_definition) {
78     return new static(
79       $configuration,
80       $plugin_id,
81       $plugin_definition,
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')
89     );
90   }
91
92   /**
93    * @inheritdoc
94    */
95   public function getDataSets() {
96     $data_sets = [];
97     $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
98
99     foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) {
100       if (isset($sitemap_entity_types[$entity_type_name])) {
101
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) {
106             continue 2;
107           }
108         }
109
110         foreach ($bundles as $bundle_name => $bundle_settings) {
111           if ($bundle_settings['index']) {
112             $data_sets[] = [
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(),
117             ];
118           }
119         }
120       }
121     }
122
123     return $data_sets;
124   }
125
126   /**
127    * @inheritdoc
128    */
129   protected function processDataSet($entity) {
130
131     $entity_id = $entity->id();
132     $entity_type_name = $entity->getEntityTypeId();
133
134     $entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
135
136     if (empty($entity_settings['index'])) {
137       return FALSE;
138     }
139
140     $url_object = $entity->toUrl();
141
142     // Do not include external paths.
143     if (!$url_object->isRouted()) {
144       return FALSE;
145     }
146
147     $path = $url_object->getInternalPath();
148
149     // Do not include paths that have been already indexed.
150     if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
151       return FALSE;
152     }
153
154     $url_object->setOption('absolute', TRUE);
155
156     return [
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)
163         : [],
164
165       // Additional info useful in hooks.
166       'meta' => [
167         'path' => $path,
168         'entity_info' => [
169           'entity_type' => $entity_type_name,
170           'id' => $entity_id,
171         ],
172       ]
173     ];
174   }
175
176   /**
177    * @inheritdoc
178    */
179   protected function getBatchIterationElements($entity_info) {
180     $query = $this->entityTypeManager->getStorage($entity_info['entity_type_name'])->getQuery();
181
182     if (!empty($entity_info['keys']['id'])) {
183       $query->sort($entity_info['keys']['id'], 'ASC');
184     }
185     if (!empty($entity_info['keys']['bundle'])) {
186       $query->condition($entity_info['keys']['bundle'], $entity_info['bundle_name']);
187     }
188     if (!empty($entity_info['keys']['status'])) {
189       $query->condition($entity_info['keys']['status'], 1);
190     }
191
192     if ($this->needsInitialization()) {
193       $count_query = clone $query;
194       $this->initializeBatch($count_query->count()->execute());
195     }
196
197     if ($this->isBatch()) {
198       $query->range($this->context['sandbox']['progress'], $this->batchSettings['batch_process_limit']);
199     }
200
201     return $this->entityTypeManager
202       ->getStorage($entity_info['entity_type_name'])
203       ->loadMultiple($query->execute());
204   }
205 }