Commit 262c53f5 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2865973 by laue, gbyte.co: links.menu definitions are not added to the sitemap

parent df0ab619
......@@ -2,6 +2,16 @@
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Menu\MenuLinkTree;
/**
* Class EntityMenuLinkContentUrlGenerator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
......@@ -14,60 +24,121 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
* weight = 5,
* settings = {
* "instantiate_for_each_data_set" = true,
* "overrides_entity_type" = "menu_link_content",
* },
* )
*/
class EntityMenuLinkContentUrlGenerator extends EntityUrlGenerator {
class EntityMenuLinkContentUrlGenerator extends UrlGeneratorBase {
/**
* @var \Drupal\Core\Menu\MenuLinkTree
*/
protected $menuLinkTree;
/**
* EntityMenuLinkContentUrlGenerator constructor.
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\simple_sitemap\Logger $logger
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
* @param \Drupal\Core\Menu\MenuLinkTree $menu_link_tree
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Simplesitemap $generator,
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
EntityTypeManagerInterface $entity_type_manager,
Logger $logger,
EntityHelper $entityHelper,
MenuLinkTree $menu_link_tree
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$generator,
$sitemap_generator,
$language_manager,
$entity_type_manager,
$logger,
$entityHelper
);
$this->menuLinkTree = $menu_link_tree;
}
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.sitemap_generator'),
$container->get('language_manager'),
$container->get('entity_type.manager'),
$container->get('simple_sitemap.logger'),
$container->get('simple_sitemap.entity_helper'),
$container->get('menu.link_tree')
);
}
/**
* @inheritdoc
*/
public function getDataSets() {
$data_sets = [];
$menu_names = [];
$bundle_settings = $this->generator->getBundleSettings();
if (!empty($bundle_settings['menu_link_content'])) {
$keys = $this->entityTypeManager->getDefinition('menu_link_content')->getKeys();
$keys['bundle'] = 'menu_name'; // Menu fix.
foreach ($bundle_settings['menu_link_content'] as $bundle_name => $settings) {
if ($settings['index']) {
$data_sets[] = [
'bundle_settings' => $settings,
'bundle_name' => $bundle_name,
'entity_type_name' => 'menu_link_content',
'keys' => $keys,
];
$menu_names[] = $bundle_name;
}
}
}
return $data_sets;
return $menu_names;
}
/**
* @inheritdoc
*/
protected function processDataSet($entity) {
protected function processDataSet($link) {
$entity_id = $entity->id();
$entity_type_name = $entity->getEntityTypeId();
if (!$link->isEnabled()) {
return FALSE;
}
$entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
$url_object = $link->getUrlObject();
if (empty($entity_settings['index'])) {
// Do not include external paths.
if (!$url_object->isRouted()) {
return FALSE;
}
if (!$entity->isEnabled()) {
return FALSE;
// If not a menu_link_content link, use bundle settings.
$meta_data = $link->getMetaData();
if (empty($meta_data['entity_id'])) {
$entity_settings = $this->generator->getBundleSettings('menu_link_content', $link->getMenuName());
}
$url_object = $entity->getUrlObject();
// If menu link is of entity type menu_link_content, take under account its entity override.
else {
$entity_settings = $this->generator->getEntityInstanceSettings('menu_link_content', $meta_data['entity_id']);
// Do not include external paths.
if (!$url_object->isRouted()) {
return FALSE;
if (empty($entity_settings['index'])) {
return FALSE;
}
}
$path = $url_object->getInternalPath();
......@@ -79,23 +150,54 @@ class EntityMenuLinkContentUrlGenerator extends EntityUrlGenerator {
$url_object->setOption('absolute', TRUE);
return [
$entity = $this->entityHelper->getEntityFromUrlObject($url_object);
$path_data = [
'url' => $url_object,
'lastmod' => NULL,
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images']) //todo check if this is working for menu links
? $this->getImages($entity_type_name, $entity_id)
'images' => !empty($entity_settings['include_images']) && !empty($entity)
? $this->getImages($entity->getEntityTypeId(), $entity->id())
: [],
// Additional info useful in hooks.
'meta' => [
'path' => $path,
'entity_info' => [
'entity_type' => $entity_type_name,
'id' => $entity_id,
],
]
];
if (!empty($entity)) {
$path_data['meta']['entity_info'] = [
'entity_type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
];
}
return $path_data;
}
/**
* @inheritdoc
*/
protected function getBatchIterationElements($menu_name) {
// Retrieve the expanded tree.
$tree = $this->menuLinkTree->load($menu_name, new MenuTreeParameters());
$tree = $this->menuLinkTree->transform($tree, [['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']]);
$elements = [];
foreach ($tree as $i => $item) {
$elements[] = $item->link;
}
$elements = array_values($elements);
if ($this->needsInitialization()) {
$this->initializeBatch(count($elements));
}
return $this->isBatch()
? array_slice($elements, $this->context['sandbox']['progress'], $this->batchSettings['batch_process_limit'])
: $elements;
}
}
......@@ -102,7 +102,8 @@ class EntityUrlGenerator extends UrlGeneratorBase {
// Skip this entity type if another plugin is written to override its generation.
foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
if ($plugin['enabled'] && $plugin['id'] === 'entity_' . $entity_type_name) {
if ($plugin['enabled'] && !empty($plugin['settings']['overrides_entity_type'])
&& $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
continue 2;
}
}
......@@ -176,7 +177,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
/**
* @inheritdoc
*/
protected function getBatchIterationElements(array $entity_info) {
protected function getBatchIterationElements($entity_info) {
$query = $this->entityTypeManager->getStorage($entity_info['entity_type_name'])->getQuery();
if (!empty($entity_info['keys']['id'])) {
......
......@@ -389,10 +389,10 @@ abstract class UrlGeneratorBase extends SimplesitemapPluginBase implements UrlGe
}
/**
* @param array $elements
* @param mixed $elements
* @return array
*/
protected function getBatchIterationElements(array $elements) {
protected function getBatchIterationElements($elements) {
if ($this->needsInitialization()) {
$this->initializeBatch(count($elements));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment