Commit 9cd17a3f authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2931911 by gbyte.co: Allow overriding entity generator plugin for specific entity types

parent bb0fbae5
......@@ -24,12 +24,41 @@ class UrlGenerator extends Plugin {
public $id;
/**
* The human-readable name of the plugin.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $title;
/**
* A short description of the plugin.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
/**
* An integer to determine the weight of this generator relative to others.
*
* @var int
*/
public $weight;
/**
* @var bool
* Whether this plugin is enabled or disabled by default.
*
* @var bool (optional)
*/
public $enabled = TRUE;
/**
* The default settings for the plugin.
*
* @var array (optional)
*/
public $instantiateForEachDataSet;
public $settings = [];
}
......@@ -187,7 +187,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function generateSitemap(array &$form, FormStateInterface $form_state) {
$this->generator->generateSitemap();
$this->generator->generateSitemap('nobatch');
}
}
......@@ -17,8 +17,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*
* @UrlGenerator(
* id = "arbitrary",
* title = @Translation("Arbitrary URL generator"),
* description = @Translation("Generates URLs from data sets collected in the hook_arbitrary_links_alter hook."),
* enabled = TRUE,
* weight = 20,
* instantiateForEachDataSet = false
* settings = {
* },
* )
*/
class ArbitraryUrlGenerator extends UrlGeneratorBase {
......
......@@ -19,9 +19,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*
* @UrlGenerator(
* id = "custom",
* title = @Translation("Custom URL generator"),
* description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
* enabled = TRUE,
* weight = 0,
* instantiateForEachDataSet = false
* settings = {
* },
* )
*
*/
class CustomUrlGenerator extends UrlGeneratorBase {
......
<?php
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
/**
* Class EntityMenuLinkContentUrlGenerator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*
* @UrlGenerator(
* id = "entity_menu_link_content",
* title = @Translation("Menu link URL generator"),
* description = @Translation("Generates menu link URLs by overriding the 'entity' URL generator."),
* enabled = TRUE,
* weight = 5,
* settings = {
* "instantiate_for_each_data_set" = true,
* },
* )
*/
class EntityMenuLinkContentUrlGenerator extends EntityUrlGenerator {
/**
* @inheritdoc
*/
public function getDataSets() {
$data_sets = [];
$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,
];
}
}
}
return $data_sets;
}
/**
* @inheritdoc
*/
protected function processDataSet($entity) {
$entity_id = $entity->id();
$entity_type_name = $entity->getEntityTypeId();
$entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
if (empty($entity_settings['index'])) {
return FALSE;
}
if (!$entity->isEnabled()) {
return FALSE;
}
$url_object = $entity->getUrlObject();
// Do not include external paths.
if (!$url_object->isRouted()) {
return FALSE;
}
$path = $url_object->getInternalPath();
// Do not include paths that have been already indexed.
if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
return FALSE;
}
$url_object->setOption('absolute', TRUE);
return [
'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)
: [],
// Additional info useful in hooks.
'meta' => [
'path' => $path,
'entity_info' => [
'entity_type' => $entity_type_name,
'id' => $entity_id,
],
]
];
}
}
......@@ -2,30 +2,110 @@
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 Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class EntityUrlGenerator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*
* @UrlGenerator(
* id = "entity",
* title = @Translation("Entity URL generator"),
* description = @Translation("Generates URLs for entity bundles and bundle overrides."),
* enabled = TRUE,
* weight = 10,
* instantiateForEachDataSet = true
* settings = {
* "instantiate_for_each_data_set" = true,
* },
* )
*/
class EntityUrlGenerator extends UrlGeneratorBase {
/**
* @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
*/
protected $urlGeneratorManager;
/**
* 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\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
*/
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,
UrlGeneratorManager $url_generator_manager
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$generator,
$sitemap_generator,
$language_manager,
$entity_type_manager,
$logger,
$entityHelper
);
$this->urlGeneratorManager = $url_generator_manager;
}
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('plugin.manager.simple_sitemap.url_generator')
);
}
/**
* @inheritdoc
*/
public function getDataSets() {
$data_sets = [];
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
// Menu fix.
$keys['bundle'] = $entity_type_name === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
// 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) {
continue 2;
}
}
foreach ($bundles as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) {
......@@ -33,12 +113,13 @@ class EntityUrlGenerator extends UrlGeneratorBase {
'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name,
'entity_type_name' => $entity_type_name,
'keys' => $keys,
'keys' => $sitemap_entity_types[$entity_type_name]->getKeys(),
];
}
}
}
}
return $data_sets;
}
......@@ -56,19 +137,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
return FALSE;
}
switch ($entity_type_name) {
// Loading url object for menu links.
case 'menu_link_content':
if (!$entity->isEnabled()) {
return FALSE;
}
$url_object = $entity->getUrlObject();
break;
// Loading url object for other entities.
default:
$url_object = $entity->toUrl();
}
$url_object = $entity->toUrl();
// Do not include external paths.
if (!$url_object->isRouted()) {
......@@ -84,14 +153,9 @@ class EntityUrlGenerator extends UrlGeneratorBase {
$url_object->setOption('absolute', TRUE);
$lastmod = method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL;
// Menu fix.
$lastmod = $entity_type_name !== 'menu_link_content' ? $lastmod : NULL;
return [
'url' => $url_object,
'lastmod' => $lastmod,
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : 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'])
......
......@@ -252,13 +252,15 @@ class Simplesitemap {
});
foreach ($plugins as $plugin) {
if ($plugin['instantiateForEachDataSet']) {
foreach ($this->urlGeneratorManager->createInstance($plugin['id'])->getDataSets() as $data_sets) {
$this->batch->addOperation($plugin['id'], $data_sets);
if ($plugin['enabled']) {
if (!empty($plugin['settings']['instantiate_for_each_data_set'])) {
foreach ($this->urlGeneratorManager->createInstance($plugin['id'])->getDataSets() as $data_sets) {
$this->batch->addOperation($plugin['id'], $data_sets);
}
}
else {
$this->batch->addOperation($plugin['id']);
}
}
else {
$this->batch->addOperation($plugin['id']);
}
}
......
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