Skip to content
Snippets Groups Projects
Commit 20c3659e authored by Owen Bush's avatar Owen Bush Committed by Owen Bush
Browse files

Issue #3298679 by owenbush: By updating a series, it deletes all the...

Issue #3298679 by owenbush: By updating a series, it deletes all the eventinstances and recreates them,  which deletes all information stored on the instance
parent 6d0546cb
No related branches found
Tags 2.0.0-beta3
No related merge requests found
......@@ -44,6 +44,9 @@ recurring_events.eventseries.config:
threshold_prevent_save:
type: integer
label: 'Prevent saving a series if too many instances are being created'
creator_plugin:
type: string
label: 'The creator plugin used when creating event instances'
recurring_events.eventinstance.config:
type: config_object
......
......@@ -5,6 +5,10 @@
* Custom hooks exposed by the recurring_events module.
*/
use Drupal\recurring_events\Entity\EventSeries;
use Drupal\recurring_events\EventInstanceCreatorInterface;
use Drupal\recurring_events\EventInstanceCreatorPluginManager;
/**
* Alter the time options available when creating an event series entity.
*
......@@ -71,6 +75,23 @@ function hook_recurring_events_event_instance_alter(array &$event_instance = [])
$event_instance['event_series_id'] = 12;
}
/**
* Alter the active EventInstanceCreator plugin.
*
* @param Drupal\recurring_events\EventInstanceCreatorInterface $active_plugin
* The active plugin to use.
* @param Drupal\recurring_events\EventInstanceCreatorPluginManager $plugin_manager
* The plugin manager to discover plugins.
* @param Drupal\recurring_events\Entity\EventSeries $series
* The event series for which we need to create instances.
*/
function hook_recurring_events_event_instance_creator_plugin_alter(EventInstanceCreatorInterface &$active_plugin, EventInstanceCreatorPluginManager $plugin_manager, EventSeries $series) {
// If this is series #1 then use some-other-id plugin instead.
if ($series->id() === 1) {
$active_plugin = $plugin_manager->createInstance('some-other-id', []);
}
}
/**
* Alter the form config array after it has been generated.
*
......
......@@ -649,3 +649,12 @@ function recurring_events_update_8013() {
}
return t('No Recurring events views to update.');
}
/**
* Set the default event instance plugin creator.
*/
function recurring_events_update_8014() {
$config = \Drupal::configFactory()->getEditable('recurring_events.eventseries.config');
$config->set('creator_plugin', 'recurring_events_eventinstance_recreator');
$config->save(TRUE);
}
......@@ -219,8 +219,11 @@ function recurring_events_eventseries_update(EntityInterface $entity) {
if ($date_changes) {
if ($entity->isPublished() || !$moderated) {
if ($entity->isDefaultTranslation()) {
$creation_service->clearEventInstances($entity);
$creation_service->createInstances($entity);
$plugin_manager = \Drupal::service('plugin.manager.event_instance_creator');
$config = \Drupal::config('recurring_events.eventseries.config');
$active_plugin = $plugin_manager->createInstance($config->get('creator_plugin'), []);
\Drupal::moduleHandler()->alter('recurring_events_event_instance_creator_plugin', $active_plugin, $plugin_manager, $entity);
$active_plugin->processInstances($entity);
}
}
// Get a fresh version of the series to get the updated instances.
......
......@@ -7,3 +7,6 @@ services:
recurring_events.event_creation_service:
class: Drupal\recurring_events\EventCreationService
arguments: ['@string_translation', '@database', '@logger.factory', '@messenger', '@plugin.manager.field.field_type', '@entity_field.manager', '@module_handler', '@entity_type.manager', '@keyvalue']
plugin.manager.event_instance_creator:
class: Drupal\recurring_events\EventInstanceCreatorPluginManager
parent: default_plugin_manager
<?php
namespace Drupal\recurring_events\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a EventInstanceCreator annotation object.
*
* @Annotation
*/
class EventInstanceCreator extends Plugin {
/**
* Description of plugin
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $description;
}
<?php
namespace Drupal\recurring_events;
use Drupal\Component\Plugin\PluginBase;
use Drupal\recurring_events\Entity\EventSeries;
use Drupal\recurring_events\EventCreationService;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A base class for EventInstanceCreator plugins.
*
* @see \Drupal\recurring_events\Annotation\EventInstanceCreator
* @see \Drupal\recurring_events\EventInstanceCreatorInterface
*/
abstract class EventInstanceCreatorBase extends PluginBase implements EventInstanceCreatorInterface {
/**
* The event creation service.
*
* @var \Drupal\recurring_events\EventCreationService
*/
protected EventCreationService $creationService;
/**
* Constructs a \Drupal\recurring_events\EventInstanceCreatorBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\recurring_events\EventCreationService $creation_service
* The event creation service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventCreationService $creation_service) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->creationService = $creation_service;
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('recurring_events.event_creation_service')
);
}
/**
* {@inheritDoc}
*/
public function description() {
// Retrieve the @description property from the annotation and return it.
return $this->pluginDefinition['description'];
}
/**
* {@inheritDoc}
*/
abstract public function processInstances(EventSeries $series);
}
<?php
namespace Drupal\recurring_events;
use Drupal\recurring_events\Entity\EventSeries;
/**
* An interface for all EventInstanceCreator type plugins.
*/
interface EventInstanceCreatorInterface {
/**
* Provide a description of the plugin.
*
* @return string
* A string description of the plugin.
*/
public function description();
/**
* Process the instances for a particular series.
*
* @param EventSeries $series
* The series for which to process instances.
*/
public function processInstances(EventSeries $series);
}
<?php
namespace Drupal\recurring_events;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Provides an EventInstanceCreator plugin manager.
*
* @see \Drupal\recurring_events\Annotation\EventInstanceCreator
* @see \Drupal\recurring_events\EventInstanceCreatorInterface
* @see plugin_api
*/
class EventInstanceCreatorPluginManager extends DefaultPluginManager {
/**
* Constructs a EventInstanceCreatorPluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/EventInstanceCreator',
$namespaces,
$module_handler,
'Drupal\recurring_events\EventInstanceCreatorInterface',
'Drupal\recurring_events\Annotation\EventInstanceCreator'
);
$this->alterInfo('eventinstance_creator_info');
$this->setCacheBackend($cache_backend, 'eventinstance_creator_info_plugins');
}
}
......@@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\recurring_events\EventCreationService;
use Drupal\recurring_events\EventInstanceCreatorPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -23,14 +24,24 @@ class EventSeriesSettingsForm extends ConfigFormBase {
*/
protected $creationService;
/**
* The event instance creator plugin manager.
*
* @var \Drupal\recurring_events\EventInstanceCreatorPluginManager
*/
protected $eventInstanceCreatorManager;
/**
* Constructs a new EventSeriesSettingsForm.
*
* @param \Drupal\recurring_events\EventCreationService $creation_service
* The event creation service.
* @param \Drupal\recurring_events\EventInstanceCreatorPluginManager $creator_manager
* The event creation service.
*/
public function __construct(EventCreationService $creation_service) {
public function __construct(EventCreationService $creation_service, EventInstanceCreatorPluginManager $creator_manager) {
$this->creationService = $creation_service;
$this->eventInstanceCreatorManager = $creator_manager;
}
/**
......@@ -38,7 +49,8 @@ class EventSeriesSettingsForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('recurring_events.event_creation_service')
$container->get('recurring_events.event_creation_service'),
$container->get('plugin.manager.event_instance_creator')
);
}
......@@ -102,6 +114,12 @@ class EventSeriesSettingsForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('recurring_events.eventseries.config');
$plugin_definitions = $this->eventInstanceCreatorManager->getDefinitions();
$creator_plugins = [];
foreach ($plugin_definitions as $id => $plugin) {
$creator_plugins[$id] = (string) $plugin['description'];
}
$form['creation'] = [
'#type' => 'details',
'#title' => $this->t('Event Creation'),
......@@ -246,6 +264,14 @@ class EventSeriesSettingsForm extends ConfigFormBase {
],
];
$form['creation']['creator_plugin'] = [
'#type' => 'radios',
'#title' => $this->t('Event Instance Creator Plugin'),
'#description' => $this->t('Select the plugin to use when creating event instances.'),
'#default_value' => $config->get('creator_plugin'),
'#options' => $creator_plugins,
];
$form['display'] = [
'#type' => 'details',
'#title' => $this->t('Event Display'),
......
<?php
namespace Drupal\recurring_events\Plugin\EventInstanceCreator;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\recurring_events\Entity\EventSeries;
use Drupal\recurring_events\EventInstanceCreatorBase;
/**
* Plugin that removes and recreates all event instances.
*
* @EventInstanceCreator(
* id = "recurring_events_eventinstance_recreator",
* description = @Translation("Recreate Event Instances")
* )
*/
class RecreateEventInstanceCreator extends EventInstanceCreatorBase implements ContainerFactoryPluginInterface {
/**
* {@inheritDoc}
*/
public function processInstances(EventSeries $series) {
$this->creationService->clearEventInstances($series);
$this->creationService->createInstances($series);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment