Skip to content
Snippets Groups Projects
Commit 37942bbf authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #3520853: More usage of $transition->setEntityWorkflowField()

parent 82b59007
No related branches found
No related tags found
No related merge requests found
Pipeline #504297 passed with warnings
......@@ -386,7 +386,7 @@ class WorkflowTransitionElement extends FormElement {
// Note: when editing existing Transition, user may still change comments.
// Note: subfields might be disabled, and not exist in formState.
// Note:subfields are already set by core.
// Note: subfields are already set by core.
// This is only needed on Node edit widget, not on Node view/history page.
$comment = $values['comment'][0]['value'] ?? '';
$timestamp_input = $form_state->getUserInput()['timestamp'][0]['value'] ?? FALSE;
......
......@@ -685,29 +685,30 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
return $from_sid;
}
// Save the (scheduled) transition.
if ($this->isScheduled()) {
$this->save();
$sid = $from_sid;
}
elseif ($this->isExecuted()) {
$sid = match (TRUE) {
$this->isScheduled()
// Save the (scheduled) transition. $sid is always $from_sid.
=> $this->save() ? $from_sid : $from_sid,
$this->isExecuted()
// We create a new transition, or update an existing one.
// Do not update the entity itself.
// Validate transition, save in history table and delete from schedule table.
$sid = $this->execute($force);
}
else {
// Update targetEntity's itemList with the workflow field in two formats.
$this->setEntityWorkflowField();
$this->setEntityChangedTime();
$sid = $entity->save() ? $to_sid : $from_sid;
}
=> $this->execute($force),
default
// Update targetEntity's itemList with the workflow field in two formats.
=> $this
->setEntityWorkflowField()
->setEntityChangedTime()
->getTargetEntity()
->save() ? $to_sid : $from_sid,
};
return $sid;
}
/**
* Updates the entity's workflow field with value and transition.
* {@inheritdoc}
*
* @todo Also update entity with additional fields.
*/
......@@ -728,16 +729,16 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
// Exception has occurred. Error: Call to a member function setValue() on null
// Happens when adding CommentWithWorkflow to mismatched Node.
$message = $this->t('A comment with Workflow field is added to a Content type. Both %entity_type_id and Comment must share the same field name %field_name, or else the comment value cannot be added to the %entity_type_id.',
['%field_name' => $field_name,
'%entity_type_id' =>$entity->getEntityTypeId(),
]);
['%field_name' => $field_name,
'%entity_type_id' => $entity->getEntityTypeId(),
]);
$this->messenger()->addError($message);
}
return $this;
}
/**
* Update the Entity's ChangedTime when the option is set.
* {@inheritdoc}
*/
public function setEntityChangedTime(): WorkflowTransitionInterface {
if ($this->getWorkflow()->getSetting('always_update_entity')) {
......
......@@ -148,6 +148,16 @@ interface WorkflowTransitionInterface extends WorkflowConfigTransitionInterface,
*/
public function getTargetEntityTypeId();
/**
* Updates the entity's workflow field with value and transition.
*/
public function setEntityWorkflowField(): WorkflowTransitionInterface;
/**
* Update the Entity's ChangedTime when the option is set.
*/
public function setEntityChangedTime(): WorkflowTransitionInterface;
/**
* {@inheritdoc}
*/
......
......@@ -384,24 +384,31 @@ class WorkflowEntityHooks {
// situation, that generates message "Transition is executed twice".
$transition->isExecuted()
=> TRUE,
// Scheduled transitions must be saved, without updating the entity.
$transition->isScheduled()
// Scheduled transitions must be saved, without updating the entity.
=> $transition->save(),
$entity->getEntityTypeId() == 'comment'
// If Transition is added via CommentForm, save Comment AND Entity.
// Execute and check the result.
// @todo Add setEntityChangedTime() on node (not on comment).
$entity->getEntityTypeId() == 'comment'
=> ($to_sid === $transition->setEntityChangedTime()->executeAndUpdateEntity()),
// Execute and check the result.
=> ($to_sid === $transition
->setEntityChangedTime()
->executeAndUpdateEntity()),
default
=> ($to_sid === $transition->setEntityChangedTime()->execute()),
// Execute and check the result.
=> ($to_sid === $transition
->setEntityChangedTime()
->execute()),
};
// If the transition failed, revert the entity workflow status.
// For new entities, we do nothing: it has no original.
if (!$executed && $original_entity) {
$original_value = $original_entity->{$field_name}->value;
$entity->{$field_name}->setValue($original_value);
$original_sid = $original_entity->{$field_name}->value;
$entity->{$field_name}->setValue($original_sid);
}
}
......
......@@ -130,8 +130,7 @@ class WorkflowDefaultWidget extends WidgetBase {
// And also update the above created $transition.
$values = $this->massageFormValues($values, $form, $form_state);
// Update targetEntity's itemList with the workflow field in two formats.
// In other locations, $transition->setEntityWorkflowField() is used.
$items->setValue($transition);
$transition->setEntityWorkflowField();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment