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

Issue #3508157: New transition - better use of...

Issue #3508157: New transition - better use of WorkflowTransition::baseFieldDefinition::DefaultValueCallback
parent 67b2a636
No related branches found
No related tags found
No related merge requests found
Pipeline #455658 passed with warnings
......@@ -183,7 +183,7 @@ class WorkflowTransitionElement extends FormElement {
// When not $show_widget, the 'from_sid' is displayed.
'#access' => $show_widget,
// @todo This is only needed for 'radios'. Why?
'#default_value' => $a = reset($element[$attribute_name][$attribute_key]['#default_value']),
'#default_value' => reset($element[$attribute_name][$attribute_key]['#default_value']),
];
// Subfield is NEVER disabled in Workflow 'Manage form display' settings.
// @see addPreSaveBasefieldWarning()
......
......@@ -294,22 +294,11 @@ class WorkflowTargetEntity {
$field_name) ?? $transition;
// Note: Field is empty if node created before module installation.
$transition ??= $entity->{$field_name}[0]
? $entity->{$field_name}[0]->getTransition()
: NULL;
$item = $entity->{$field_name}[0] ?? NULL;
$transition ??= $item ? $item->getTransition() : NULL;
// For VBO, a dummy node has been created.
if (!$transition) {
// Create a transition, to pass to the form.
foreach (_workflow_info_fields($entity, NULL, NULL, $field_name) as $definition) {
$wid = $definition->getSetting('workflow_type');
}
$current_sid = '';
$transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name, 'wid' => $wid])
->setTargetEntity($entity)
->setValues($current_sid);
}
$transition ??= WorkflowTransition::create(['entity' => $entity, 'field_name' => $field_name]);
return $transition;
}
......
......@@ -169,6 +169,11 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
/**
* {@inheritdoc}
*
* @param array $values
* An array of values to set, keyed by property name.
* A value for the 'field_name' is required.
* Also either state ID ('from_sid') or targetEntity ('entity').
*/
public static function create(array $values = []) {
$transition = NULL;
......@@ -181,15 +186,21 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
}
if ($state instanceof WorkflowState) {
// Additional default values are defined in baseFieldDefinitions().
/** @var \Drupal\workflow\Entity\WorkflowState $state */
if (!isset($values['wid'])) {
$values['wid'] = $state ? $state->getWorkflowId() : '';
}
$values['from_sid'] = $state ? $state->id() : '';
$transition = parent::create($values);
$values['wid'] ??= $state ? $state->getWorkflowId() : '';
$values['from_sid'] ??= $state ? $state->id() : '';
}
$entity = $values['entity'] ?? NULL;
if ($entity) {
$field_name = $values['field_name'];
$values['entity_type'] = $entity->getEntityTypeId();
$values['entity_id'] = $entity->id();
$values['wid'] = $entity->{$field_name}->getSetting('workflow_type');
}
// Additional default values are defined in baseFieldDefinitions().
$transition = parent::create($values);
return $transition;
}
......@@ -832,15 +843,19 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
/**
* {@inheritdoc}
*
* @see workflow_node_current_state
*/
public static function getDefaultStateId(WorkflowTransition $transition, BaseFieldDefinition $definition) {
$sid = '';
switch ($definition->getName()) {
case 'from_sid':
// Not implemented yet, since from_sid is given upon creation.
$entity = $transition->getTargetEntity();
$field_name = $transition->getFieldName();
$sid = workflow_node_current_state($entity, $field_name);
if (!$sid) {
\Drupal::logger('workflow_action')->notice('Unable to get current workflow state of entity %id.', ['%id' => $entity->id()]);
return NULL;
}
break;
case 'to_sid':
......@@ -1061,7 +1076,6 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
// Do not change. @see https://www.drupal.org/project/drupal/issues/2643308
->setSetting('allowed_values_function', 'workflow_state_allowed_values')
->setDefaultValueCallback(static::class . '::getDefaultStateId')
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
......@@ -1075,7 +1089,7 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition
->setCardinality(1)
// Do not change. @see https://www.drupal.org/project/drupal/issues/2643308
->setSetting('allowed_values_function', 'workflow_state_allowed_values')
->setDefaultValueCallback(static::class . '::getDefaultStateId')
->setDefaultValueCallback(static::class . '::getDefaultStateId')
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
......
......@@ -86,11 +86,6 @@ abstract class WorkflowStateActionBase extends ConfigurableActionBase implements
$config = $this->configuration;
$field_name = workflow_get_field_name($entity, $config['field_name']);
$current_sid = workflow_node_current_state($entity, $field_name);
if (!$current_sid) {
\Drupal::logger('workflow_action')->notice('Unable to get current workflow state of entity %id.', ['%id' => $entity_id]);
return NULL;
}
$to_sid = $config['to_sid'] ?? '';
// Get the Comment. Parse the $comment variables.
......@@ -103,8 +98,7 @@ abstract class WorkflowStateActionBase extends ConfigurableActionBase implements
]);
$force = $this->configuration['force'];
$transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name])
->setTargetEntity($entity)
$transition = WorkflowTransition::create(['entity' => $entity, 'field_name' => $field_name])
->setValues($to_sid, $user->id(), NULL, $comment)
->force($force);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment