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 Drupal\Core\Menu\MenuTreeParameters;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Menu\MenuLinkTree;
16 * Class EntityMenuLinkContentUrlGenerator
17 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\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."),
25 * "instantiate_for_each_data_set" = true,
26 * "overrides_entity_type" = "menu_link_content",
30 class EntityMenuLinkContentUrlGenerator extends UrlGeneratorBase {
33 * @var \Drupal\Core\Menu\MenuLinkTree
35 protected $menuLinkTree;
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
50 public function __construct(
54 Simplesitemap $generator,
55 SitemapGenerator $sitemap_generator,
56 LanguageManagerInterface $language_manager,
57 EntityTypeManagerInterface $entity_type_manager,
59 EntityHelper $entityHelper,
60 MenuLinkTree $menu_link_tree
73 $this->menuLinkTree = $menu_link_tree;
76 public static function create(
77 ContainerInterface $container,
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')
98 public function getDataSets() {
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;
115 protected function processDataSet($link) {
117 if (!$link->isEnabled()) {
121 $url_object = $link->getUrlObject();
123 // Do not include external paths.
124 if ($url_object->isExternal()) {
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());
134 // If menu link is of entity type menu_link_content, take under account its entity override.
136 $entity_settings = $this->generator->getEntityInstanceSettings('menu_link_content', $meta_data['entity_id']);
138 if (empty($entity_settings['index'])) {
143 // There can be internal paths that are not rooted, like 'base:/path'.
144 if ($url_object->isRouted()) {
145 $path = $url_object->getInternalPath();
147 else { // Handle base scheme.
148 if (strpos($uri = $url_object->toUriString(), 'base:/') === 0 ) {
149 $path = $uri[6] === '/' ? substr($uri, 7) : substr($uri, 6);
151 else { // Handle unforeseen schemes.
156 // Do not include paths that have been already indexed.
157 if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
161 $url_object->setOption('absolute', TRUE);
163 $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
166 'url' => $url_object,
167 'lastmod' => !empty($entity) && method_exists($entity, 'getChangedTime')
168 ? date_iso8601($entity->getChangedTime())
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())
176 // Additional info useful in hooks.
181 if (!empty($entity)) {
182 $path_data['meta']['entity_info'] = [
183 'entity_type' => $entity->getEntityTypeId(),
184 'id' => $entity->id(),
194 protected function getBatchIterationElements($menu_name) {
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']]);
201 foreach ($tree as $i => $item) {
202 $elements[] = $item->link;
204 $elements = array_values($elements);
206 if ($this->needsInitialization()) {
207 $this->initializeBatch(count($elements));
210 return $this->isBatch()
211 ? array_slice($elements, $this->context['sandbox']['progress'], $this->batchSettings['batch_process_limit'])