diff --git a/recurring_events.module b/recurring_events.module index 06990e975fbe6b68c040d12ac44e1635bad2e33a..a4570f02181c54a7b1885ff7c9618188b7dc89a6 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 5d055215731717b77db3376c120e9d2dbaeb335e..22cdfb23828a185035f101f8f12b27c0ef0f6ba2 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; + + } + }