Commit 35e4e2f3 authored by gbyte.co's avatar gbyte.co

Issue #2923551 by gbyte.co: Make generators pluggable through plugins

parent 58499a07
......@@ -110,6 +110,9 @@ Altering sitemap attributes and sitemap index attributes is possible through the
use of hook_simple_sitemap_attributes_alter(&$attributes){} and
hook_simple_sitemap_index_attributes_alter(&$index_attributes){}.
Adding new URL generator plugins and altering existing ones is possible through
the use of hook_simple_sitemap_url_generators_alter(&$generators){}.
There are API methods for altering stored inclusion settings, status queries and
programmatic sitemap generation. These include:
......
......@@ -12,7 +12,7 @@ services:
- '@date.formatter'
- '@datetime.time'
- '@simple_sitemap.batch'
- '@module_handler'
- '@plugin.manager.simple_sitemap.url_generator'
simple_sitemap.sitemap_generator:
class: Drupal\simple_sitemap\SitemapGenerator
......@@ -40,44 +40,9 @@ services:
- '@current_user'
simple_sitemap.batch:
class: Drupal\simple_sitemap\Batch\Batch
class: Drupal\simple_sitemap\Batch
public: false
simple_sitemap.entity_url_generator:
class: Drupal\simple_sitemap\Batch\Generator\EntityUrlGenerator
public: true
arguments:
- '@simple_sitemap.generator'
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
simple_sitemap.custom_url_generator:
class: Drupal\simple_sitemap\Batch\Generator\CustomUrlGenerator
public: true
arguments:
- '@simple_sitemap.generator'
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
- '@path.validator'
simple_sitemap.arbitrary_url_generator:
class: Drupal\simple_sitemap\Batch\Generator\ArbitraryUrlGenerator
public: true
arguments:
- '@simple_sitemap.generator'
- '@simple_sitemap.sitemap_generator'
- '@language_manager'
- '@entity_type.manager'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
- '@module_handler'
simple_sitemap.logger:
class: Drupal\simple_sitemap\Logger
public: true
......@@ -90,3 +55,7 @@ services:
public: false
arguments:
- simple_sitemap
plugin.manager.simple_sitemap.url_generator:
class: Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
parent: default_plugin_manager
<?php
namespace Drupal\simple_sitemap\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a UrlGenerator item annotation object.
*
* @package Drupal\simple_sitemap\Annotation
*
* @see Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
* @see plugin_api
*
* @Annotation
*/
class UrlGenerator extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
}
<?php
namespace Drupal\simple_sitemap\Batch;
namespace Drupal\simple_sitemap;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Cache\Cache;
......@@ -30,7 +30,7 @@ class Batch {
const BATCH_INIT_MESSAGE = 'Initializing batch...';
const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
const REGENERATION_FINISHED_MESSAGE = "The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated.";
const REGENERATION_FINISHED_MESSAGE = 'The <a href="@url" target="_blank">XML sitemap</a> has been regenerated.';
const REGENERATION_FINISHED_ERROR_MESSAGE = 'The sitemap generation finished with an error.';
/**
......@@ -106,25 +106,26 @@ class Batch {
/**
* Adds an operation to the batch.
*
* @param string $processing_service
* @param string $plugin_id
*/
public function addOperation($processing_service) {
public function addOperation($plugin_id) {
$this->batch['operations'][] = [
__CLASS__ . '::generate', [$processing_service, $this->batchSettings],
__CLASS__ . '::generate', [$plugin_id, $this->batchSettings],
];
}
/**
* Batch callback function which generates URLs.
*
* @param $processing_service
* @param $plugin_id
* @param array $batch_settings
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generate($processing_service, array $batch_settings, &$context) {
\Drupal::service($processing_service)
public static function generate($plugin_id, array $batch_settings, &$context) {
\Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($plugin_id)
->setContext($context)
->setBatchSettings($batch_settings)
->generate();
......
<?php
namespace Drupal\simple_sitemap\Batch\Generator;
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
......@@ -9,17 +9,25 @@ use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class ArbitraryUrlGenerator
* @package Drupal\simple_sitemap\Batch\Generator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*
* @UrlGenerator(
* id = "arbitrary"
* )
*/
class ArbitraryUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterface {
class ArbitraryUrlGenerator extends UrlGeneratorBase {
protected $moduleHandler;
/**
* ArbitraryUrlGenerator 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
......@@ -29,6 +37,9 @@ class ArbitraryUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInte
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Simplesitemap $generator,
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
......@@ -38,6 +49,9 @@ class ArbitraryUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInte
ModuleHandler $module_handler
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$generator,
$sitemap_generator,
$language_manager,
......@@ -48,6 +62,26 @@ class ArbitraryUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInte
$this->moduleHandler = $module_handler;
}
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('module_handler')
);
}
/**
* @return array
*/
......
<?php
namespace Drupal\simple_sitemap\Batch\Generator;
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Drupal\Core\Url;
use Drupal\simple_sitemap\Annotation\UrlGenerator;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
......@@ -10,12 +11,17 @@ use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class CustomUrlGenerator
* @package Drupal\simple_sitemap\Batch\Generator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*
* @UrlGenerator(
* id = "custom"
* )
*/
class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterface {
class CustomUrlGenerator extends UrlGeneratorBase {
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE = "The custom path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users. You can review custom paths <a href='@custom_paths_url'>here</a>.";
......@@ -32,6 +38,9 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
/**
* CustomUrlGenerator 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
......@@ -41,6 +50,9 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
* @param \Drupal\Core\Path\PathValidator $path_validator
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Simplesitemap $generator,
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
......@@ -50,6 +62,9 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
PathValidator $path_validator
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$generator,
$sitemap_generator,
$language_manager,
......@@ -58,7 +73,25 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
$entityHelper
);
$this->pathValidator = $path_validator;
}
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('path.validator')
);
}
/**
......
<?php
namespace Drupal\simple_sitemap\Batch\Generator;
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
/**
* Class EntityUrlGenerator
* @package Drupal\simple_sitemap\Batch\Generator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*
* @UrlGenerator(
* id = "entity"
* )
*/
class EntityUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterface {
class EntityUrlGenerator extends UrlGeneratorBase {
/**
* @return array
......
<?php
namespace Drupal\simple_sitemap\Batch\Generator;
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -15,9 +19,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* Class UrlGeneratorBase
* @package Drupal\simple_sitemap\Batch\Generator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*/
class UrlGeneratorBase {
abstract class UrlGeneratorBase extends PluginBase implements PluginInspectionInterface, ContainerFactoryPluginInterface, UrlGeneratorInterface {
use StringTranslationTrait;
......@@ -81,6 +85,9 @@ class UrlGeneratorBase {
/**
* UrlGeneratorBase 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
......@@ -89,6 +96,9 @@ class UrlGeneratorBase {
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Simplesitemap $generator,
SitemapGenerator $sitemap_generator,
LanguageManagerInterface $language_manager,
......@@ -96,6 +106,7 @@ class UrlGeneratorBase {
Logger $logger,
EntityHelper $entityHelper
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->generator = $generator;
$this->sitemapGenerator = $sitemap_generator;
$this->languageManager = $language_manager;
......@@ -108,6 +119,24 @@ class UrlGeneratorBase {
->load(self::ANONYMOUS_USER_ID);
}
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')
);
}
/**
* @param $context
* @return $this
......
<?php
namespace Drupal\simple_sitemap\Batch\Generator;
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
/**
* Interface UrlGeneratorInterface
* @package Drupal\simple_sitemap\Batch\Generator
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*/
interface UrlGeneratorInterface {
......
<?php
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Class UrlGeneratorManager
* @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
*/
class UrlGeneratorManager extends DefaultPluginManager {
/**
* UrlGeneratorManager constructor.
* @param \Traversable $namespaces
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
*/
public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler
) {
parent::__construct(
'Plugin/simple_sitemap/UrlGenerator',
$namespaces,
$module_handler,
'Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorInterface',
'Drupal\simple_sitemap\Annotation\UrlGenerator'
);
$this->alterInfo('simple_sitemap_url_generators');
$this->setCacheBackend($cache_backend, 'simple_sitemap:url_generator');
}
}
......@@ -8,8 +8,7 @@ use Drupal\Core\Path\PathValidator;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Component\Datetime\Time;
use Drupal\simple_sitemap\Batch\Batch;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager;
/**
* Class Simplesitemap
......@@ -58,7 +57,7 @@ class Simplesitemap {
protected $time;
/**
* @var \Drupal\simple_sitemap\Batch\Batch
* @var \Drupal\simple_sitemap\Batch
*/
protected $batch;
......@@ -67,6 +66,11 @@ class Simplesitemap {
*/
protected $moduleHandler;
/**
* @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
*/
protected $urlGeneratorManager;
/**
* @var array
*/
......@@ -101,8 +105,8 @@ class Simplesitemap {
* @param \Drupal\Core\Path\PathValidator $pathValidator
* @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
* @param \Drupal\Component\Datetime\Time $time
* @param \Drupal\simple_sitemap\Batch\Batch $batch
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* @param \Drupal\simple_sitemap\Batch $batch
* @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $urlGeneratorManager
*/
public function __construct(
SitemapGenerator $sitemapGenerator,
......@@ -114,7 +118,7 @@ class Simplesitemap {
DateFormatter $dateFormatter,
Time $time,
Batch $batch,
ModuleHandler $module_handler
UrlGeneratorManager $urlGeneratorManager
) {
$this->sitemapGenerator = $sitemapGenerator;
$this->entityHelper = $entityHelper;
......@@ -125,7 +129,7 @@ class Simplesitemap {
$this->dateFormatter = $dateFormatter;
$this->time = $time;
$this->batch = $batch;
$this->moduleHandler = $module_handler;
$this->urlGeneratorManager = $urlGeneratorManager;
}
/**
......@@ -245,10 +249,8 @@ class Simplesitemap {
'from' => $from,
]);
$this->moduleHandler->alter('simple_sitemap_generator_services', self::$generatorServices);
foreach (self::$generatorServices as $service) {
$this->batch->addOperation($service);
foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
$this->batch->addOperation($plugin['id']);
}
$this->batch->start();
......
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