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

Issue #3178669 by owenbush: Publishing Series Workflow Should Be Reflected in Instances

parent f42c6001
No related branches found
No related tags found
No related merge requests found
...@@ -173,9 +173,11 @@ function recurring_events_eventseries_insert(EntityInterface $entity) { ...@@ -173,9 +173,11 @@ function recurring_events_eventseries_insert(EntityInterface $entity) {
if (empty($instance->eventseries_id->target_id)) { if (empty($instance->eventseries_id->target_id)) {
$instance->set('eventseries_id', $entity->id()); $instance->set('eventseries_id', $entity->id());
$instance->setNewRevision(FALSE); $instance->setNewRevision(FALSE);
$instance->save();
$creation_service->configureDefaultInheritances($instance, $entity->id()); $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) { ...@@ -221,10 +223,23 @@ function recurring_events_field_inheritance_update(EntityInterface $entity) {
function recurring_events_eventseries_update(EntityInterface $entity) { function recurring_events_eventseries_update(EntityInterface $entity) {
$creation_service = \Drupal::service('recurring_events.event_creation_service'); $creation_service = \Drupal::service('recurring_events.event_creation_service');
$instances = $entity->event_instances->referencedEntities(); $instances = $entity->event_instances->referencedEntities();
$updated_statuses = $skipped_statuses = 0;
if (!empty($instances)) { if (!empty($instances)) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
$creation_service->configureDefaultInheritances($instance, $entity->id()); $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,
]));
} }
} }
......
...@@ -135,7 +135,8 @@ class EventCreationService { ...@@ -135,7 +135,8 @@ class EventCreationService {
$container->get('plugin.manager.field.field_type'), $container->get('plugin.manager.field.field_type'),
$container->get('entity_field.manager'), $container->get('entity_field.manager'),
$container->get('module_handler'), $container->get('module_handler'),
$container->get('entity_type.manager') $container->get('entity_type.manager'),
$container->get('keyvalue')
); );
} }
...@@ -660,4 +661,63 @@ class EventCreationService { ...@@ -660,4 +661,63 @@ class EventCreationService {
return $recur_fields; 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;
}
} }
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