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

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

parent bb0fbae5
...@@ -24,12 +24,41 @@ class UrlGenerator extends Plugin { ...@@ -24,12 +24,41 @@ class UrlGenerator extends Plugin {
public $id; 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 * @var int
*/ */
public $weight; 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 { ...@@ -187,7 +187,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* @param \Drupal\Core\Form\FormStateInterface $form_state * @param \Drupal\Core\Form\FormStateInterface $form_state
*/ */
public function generateSitemap(array &$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; ...@@ -17,8 +17,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* *
* @UrlGenerator( * @UrlGenerator(
* id = "arbitrary", * 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, * weight = 20,
* instantiateForEachDataSet = false * settings = {
* },
* ) * )
*/ */
class ArbitraryUrlGenerator extends UrlGeneratorBase { class ArbitraryUrlGenerator extends UrlGeneratorBase {
......
...@@ -19,9 +19,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -19,9 +19,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* *
* @UrlGenerator( * @UrlGenerator(
* id = "custom", * id = "custom",
* title = @Translation("Custom URL generator"),
* description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
* enabled = TRUE,
* weight = 0, * weight = 0,
* instantiateForEachDataSet = false * settings = {
* },
* ) * )
*
*/ */
class CustomUrlGenerator extends UrlGeneratorBase { 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 @@ ...@@ -2,30 +2,110 @@
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator; 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 * Class EntityUrlGenerator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
* *
* @UrlGenerator( * @UrlGenerator(
* id = "entity", * id = "entity",
* title = @Translation("Entity URL generator"),
* description = @Translation("Generates URLs for entity bundles and bundle overrides."),
* enabled = TRUE,
* weight = 10, * weight = 10,
* instantiateForEachDataSet = true * settings = {
* "instantiate_for_each_data_set" = true,
* },
* ) * )
*/ */
class EntityUrlGenerator extends UrlGeneratorBase { 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 * @inheritdoc
*/ */
public function getDataSets() { public function getDataSets() {
$data_sets = []; $data_sets = [];
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes(); $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) { foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) { if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
// Menu fix. // Skip this entity type if another plugin is written to override its generation.
$keys['bundle'] = $entity_type_name === 'menu_link_content' ? 'menu_name' : $keys['bundle']; foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
if ($plugin['enabled'] && $plugin['id'] === 'entity_' . $entity_type_name) {
continue 2;
}
}
foreach ($bundles as $bundle_name => $bundle_settings) { foreach ($bundles as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) { if ($bundle_settings['index']) {
...@@ -33,12 +113,13 @@ class EntityUrlGenerator extends UrlGeneratorBase { ...@@ -33,12 +113,13 @@ class EntityUrlGenerator extends UrlGeneratorBase {
'bundle_settings' => $bundle_settings, 'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name, 'bundle_name' => $bundle_name,
'entity_type_name' => $entity_type_name, 'entity_type_name' => $entity_type_name,
'keys' => $keys, 'keys' => $sitemap_entity_types[$entity_type_name]->getKeys(),
]; ];
} }
} }
} }
} }
return $data_sets; return $data_sets;
} }
...@@ -56,19 +137,7 @@ class EntityUrlGenerator extends UrlGeneratorBase { ...@@ -56,19 +137,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
return FALSE; return FALSE;
} }
switch ($entity_type_name) { $url_object = $entity->toUrl();
// 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();
}
// Do not include external paths. // Do not include external paths.
if (!$url_object->isRouted()) { if (!$url_object->isRouted()) {
...@@ -84,14 +153,9 @@ class EntityUrlGenerator extends UrlGeneratorBase { ...@@ -84,14 +153,9 @@ class EntityUrlGenerator extends UrlGeneratorBase {
$url_object->setOption('absolute', TRUE); $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 [ return [
'url' => $url_object, 'url' => $url_object,
'lastmod' => $lastmod, 'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL, 'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL, 'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images']) 'images' => !empty($entity_settings['include_images'])
......
...@@ -252,13 +252,15 @@ class Simplesitemap { ...@@ -252,13 +252,15 @@ class Simplesitemap {
}); });
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
if ($plugin['instantiateForEachDataSet']) { if ($plugin['enabled']) {
foreach ($this->urlGeneratorManager->createInstance($plugin['id'])->getDataSets() as $data_sets) { if (!empty($plugin['settings']['instantiate_for_each_data_set'])) {
$this->batch->addOperation($plugin['id'], $data_sets); 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