6ca300b50b4f7d2e2a0e6d30ad239ab58e4d032d
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Plugin / simple_sitemap / UrlGenerator / EntityMenuLinkContentUrlGenerator.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 Drupal\Core\Menu\MenuTreeParameters;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Menu\MenuLinkTree;
14
15 /**
16  * Class EntityMenuLinkContentUrlGenerator
17  * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
18  *
19  * @UrlGenerator(
20  *   id = "entity_menu_link_content",
21  *   title = @Translation("Menu link URL generator"),
22  *   description = @Translation("Generates menu link URLs by overriding the 'entity' URL generator."),
23  *   weight = 5,
24  *   settings = {
25  *     "instantiate_for_each_data_set" = true,
26  *     "overrides_entity_type" = "menu_link_content",
27  *   },
28  * )
29  */
30 class EntityMenuLinkContentUrlGenerator extends UrlGeneratorBase {
31
32   /**
33    * @var \Drupal\Core\Menu\MenuLinkTree
34    */
35   protected $menuLinkTree;
36
37   /**
38    * EntityMenuLinkContentUrlGenerator constructor.
39    * @param array $configuration
40    * @param string $plugin_id
41    * @param mixed $plugin_definition
42    * @param \Drupal\simple_sitemap\Simplesitemap $generator
43    * @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
44    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
45    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
46    * @param \Drupal\simple_sitemap\Logger $logger
47    * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
48    * @param \Drupal\Core\Menu\MenuLinkTree $menu_link_tree
49    */
50   public function __construct(
51     array $configuration,
52     $plugin_id,
53     $plugin_definition,
54     Simplesitemap $generator,
55     SitemapGenerator $sitemap_generator,
56     LanguageManagerInterface $language_manager,
57     EntityTypeManagerInterface $entity_type_manager,
58     Logger $logger,
59     EntityHelper $entityHelper,
60     MenuLinkTree $menu_link_tree
61   ) {
62     parent::__construct(
63       $configuration,
64       $plugin_id,
65       $plugin_definition,
66       $generator,
67       $sitemap_generator,
68       $language_manager,
69       $entity_type_manager,
70       $logger,
71       $entityHelper
72     );
73     $this->menuLinkTree = $menu_link_tree;
74   }
75
76   public static function create(
77     ContainerInterface $container,
78     array $configuration,
79     $plugin_id,
80     $plugin_definition) {
81     return new static(
82       $configuration,
83       $plugin_id,
84       $plugin_definition,
85       $container->get('simple_sitemap.generator'),
86       $container->get('simple_sitemap.sitemap_generator'),
87       $container->get('language_manager'),
88       $container->get('entity_type.manager'),
89       $container->get('simple_sitemap.logger'),
90       $container->get('simple_sitemap.entity_helper'),
91       $container->get('menu.link_tree')
92     );
93   }
94
95   /**
96    * @inheritdoc
97    */
98   public function getDataSets() {
99     $menu_names = [];
100     $bundle_settings = $this->generator->getBundleSettings();
101     if (!empty($bundle_settings['menu_link_content'])) {
102       foreach ($bundle_settings['menu_link_content'] as $bundle_name => $settings) {
103         if ($settings['index']) {
104           $menu_names[] = $bundle_name;
105         }
106       }
107     }
108
109     return $menu_names;
110   }
111
112   /**
113    * @inheritdoc
114    */
115   protected function processDataSet($link) {
116
117     if (!$link->isEnabled()) {
118       return FALSE;
119     }
120
121     $url_object = $link->getUrlObject();
122
123     // Do not include external paths.
124     if ($url_object->isExternal()) {
125       return FALSE;
126     }
127
128     // If not a menu_link_content link, use bundle settings.
129     $meta_data = $link->getMetaData();
130     if (empty($meta_data['entity_id'])) {
131       $entity_settings = $this->generator->getBundleSettings('menu_link_content', $link->getMenuName());
132     }
133
134     // If menu link is of entity type menu_link_content, take under account its entity override.
135     else {
136       $entity_settings = $this->generator->getEntityInstanceSettings('menu_link_content', $meta_data['entity_id']);
137
138       if (empty($entity_settings['index'])) {
139         return FALSE;
140       }
141     }
142
143     // There can be internal paths that are not rooted, like 'base:/path'.
144     if ($url_object->isRouted()) {
145      $path = $url_object->getInternalPath();
146     }
147     else { // Handle base scheme.
148       if (strpos($uri = $url_object->toUriString(), 'base:/') === 0 ) {
149         $path = $uri[6] === '/' ? substr($uri, 7) : substr($uri, 6);
150       }
151       else { // Handle unforeseen schemes.
152         $path = $uri;
153       }
154     }
155
156     // Do not include paths that have been already indexed.
157     if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
158       return FALSE;
159     }
160
161     $url_object->setOption('absolute', TRUE);
162
163     $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
164
165     $path_data = [
166       'url' => $url_object,
167       'lastmod' => !empty($entity) && method_exists($entity, 'getChangedTime')
168         ? date_iso8601($entity->getChangedTime())
169         : NULL,
170       'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
171       'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
172       'images' => !empty($entity_settings['include_images']) && !empty($entity)
173         ? $this->getImages($entity->getEntityTypeId(), $entity->id())
174         : [],
175
176       // Additional info useful in hooks.
177       'meta' => [
178         'path' => $path,
179       ]
180     ];
181     if (!empty($entity)) {
182       $path_data['meta']['entity_info'] = [
183         'entity_type' => $entity->getEntityTypeId(),
184         'id' => $entity->id(),
185       ];
186     }
187
188     return $path_data;
189   }
190
191   /**
192    * @inheritdoc
193    */
194   protected function getBatchIterationElements($menu_name) {
195
196     // Retrieve the expanded tree.
197     $tree = $this->menuLinkTree->load($menu_name, new MenuTreeParameters());
198     $tree = $this->menuLinkTree->transform($tree, [['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']]);
199
200     $elements = [];
201     foreach ($tree as $i => $item) {
202       $elements[] = $item->link;
203     }
204     $elements = array_values($elements);
205
206     if ($this->needsInitialization()) {
207       $this->initializeBatch(count($elements));
208     }
209
210     return $this->isBatch()
211       ? array_slice($elements, $this->context['sandbox']['progress'], $this->batchSettings['batch_process_limit'])
212       : $elements;
213   }
214
215 }