From 9609b6131d69aa0dad360436b5d56eeb793c0a04 Mon Sep 17 00:00:00 2001 From: owenbush <owenbush@2765259.no-reply.drupal.org> Date: Sat, 26 Dec 2020 09:18:30 -0700 Subject: [PATCH] Issue #3178669 by owenbush: Publishing Series Workflow Should Be Reflected in Instances --- recurring_events.module | 17 +++++++++- src/EventCreationService.php | 62 +++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/recurring_events.module b/recurring_events.module index 06990e97..a4570f02 100644 --- a/recurring_events.module +++ b/recurring_events.module @@ -173,9 +173,11 @@ function recurring_events_eventseries_insert(EntityInterface $entity) { if (empty($instance->eventseries_id->target_id)) { $instance->set('eventseries_id', $entity->id()); $instance->setNewRevision(FALSE); - $instance->save(); $creation_service->configureDefaultInheritances($instance, $entity->id()); + $creation_service->updateInstanceStatus($instance, $entity); + + $instance->save(); } } } @@ -221,10 +223,23 @@ function recurring_events_field_inheritance_update(EntityInterface $entity) { function recurring_events_eventseries_update(EntityInterface $entity) { $creation_service = \Drupal::service('recurring_events.event_creation_service'); $instances = $entity->event_instances->referencedEntities(); + $updated_statuses = $skipped_statuses = 0; if (!empty($instances)) { foreach ($instances as $instance) { $creation_service->configureDefaultInheritances($instance, $entity->id()); + $status_updated = $creation_service->updateInstanceStatus($instance, $entity); + if ($status_updated) { + $updated_statuses++; + $instance->save(); + } + else { + $skipped_statuses++; + } } + \Drupal::messenger()->addMessage(t('Successfully updated @success instance statuses. Skipped @skipped instances due to status or workflow mismatch with series.', [ + '@success' => $updated_statuses, + '@skipped' => $skipped_statuses, + ])); } } diff --git a/src/EventCreationService.php b/src/EventCreationService.php index 5d055215..22cdfb23 100644 --- a/src/EventCreationService.php +++ b/src/EventCreationService.php @@ -135,7 +135,8 @@ class EventCreationService { $container->get('plugin.manager.field.field_type'), $container->get('entity_field.manager'), $container->get('module_handler'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('keyvalue') ); } @@ -660,4 +661,63 @@ class EventCreationService { return $recur_fields; } + /** + * Update instance status. + * + * @param Drupal\recurring_events\Entity\EventInstance $instance + * The event instance for which to update the status. + * @param Drupal\recurring_events\Entity\EventSeries $event + * The event series entity. + */ + public function updateInstanceStatus(EventInstance $instance, EventSeries $event) { + $original_event = $event->original; + $field_name = 'status'; + + if ($this->moduleHandler->moduleExists('workflows')) { + if ($event->hasField('moderation_state') && $instance->hasField('moderation_state')) { + $series_query = $this->entityTypeManager->getStorage('workflow')->getQuery(); + $series_query->condition('type_settings.entity_types.eventseries.*', $event->bundle()); + $series_workflows = $series_query->execute(); + $series_workflows = array_keys($series_workflows); + $series_workflow = reset($series_workflows); + + $instance_query = $this->entityTypeManager->getStorage('workflow')->getQuery(); + $instance_query->condition('type_settings.entity_types.eventinstance.*', $instance->bundle()); + $instance_workflows = $instance_query->execute(); + $instance_workflows = array_keys($instance_workflows); + $instance_workflow = reset($instance_workflows); + + // We only want to mimic moderation state if the series and instance use + // the same workflows, otherwise we cannot guarantee the states match. + if ($instance_workflow === $series_workflow) { + $field_name = 'moderation_state'; + } + else { + return FALSE; + } + } + } + + $new_state = $event->get($field_name)->getValue(); + $instance_state = $instance->get($field_name)->getValue(); + + if (!empty($original_event)) { + $original_state = $original_event->get($field_name)->getValue(); + } + else { + $instance->set($field_name, $new_state); + return TRUE; + } + + // If the instance state matches the original state of the series we want + // to also update the instance state. + if ($instance_state === $original_state) { + $instance->set($field_name, $new_state); + return TRUE; + } + + return FALSE; + + } + } -- GitLab