diff --git a/src/Entity/WorkflowState.php b/src/Entity/WorkflowState.php index 70d9f4b5ff63166aeae90f5233c939c40d05eb76..b3d4ae95a48c8aaee51ca2644938ed7b9dbffc41 100644 --- a/src/Entity/WorkflowState.php +++ b/src/Entity/WorkflowState.php @@ -468,62 +468,62 @@ class WorkflowState extends ConfigEntityBase implements WorkflowStateInterface { */ public function getOptions($entity, $field_name, AccountInterface $account = NULL, $force = FALSE, $use_cache = TRUE) { $options = []; + $transitions = NULL; - // Define an Entity-specific cache per page load. - static $cache = []; - - $entity_id = ($entity) ? $entity->id() ?? '' : ''; - $entity_type_id = ($entity) ? $entity->getEntityTypeId() : ''; $current_sid = $this->id(); + // Define an Entity-specific cache per page load. + static $cache = []; // Get options from page cache, using a non-empty index (just to be sure). - $entity_index = (!$entity) ? 'x' : $entity_id; - if ($use_cache && isset($cache[$entity_type_id][$entity_index][$force][$current_sid])) { - $options = $cache[$entity_type_id][$entity_index][$force][$current_sid]; + $entity_type_index = ($entity) ? $entity->getEntityTypeId() : 'x'; + $entity_index = ($entity) ? $entity->id() ?? 'x' : 'x'; + $sid_index = $current_sid ?? 'x'; + if ($use_cache && isset($cache[$entity_type_index][$entity_index][$force][$sid_index])) { + $options = $cache[$entity_type_index][$entity_index][$force][$sid_index]; return $options; } - // No workflow, no options ;-) $workflow = $this->getWorkflow(); if (!$workflow) { + // No workflow, no options ;-) return $options; } if (!$current_sid) { - // If no State ID is given, we return all states. + // If no State ID is given (on Field settings page), we return all states. // We cannot use getTransitions, since there are no ConfigTransitions // from State with ID 0, and we do not want to repeat States. // @see https://www.drupal.org/project/workflow/issues/3119998 // @see WorkflowState::__toString(). $options = $workflow->getStates(WorkflowInterface::ACTIVE_CREATION_STATES); - return $options; + } + elseif ($current_sid) { + $transitions = $this->getTransitions($entity, $field_name, $account, $force); + } + elseif ($entity->{$field_name}->value ?? NULL) { + // Note Avoid recursive calling. + // @todo Is this code now obsolete in v1.19? + $transition = $entity->{$field_name}->first()->getTransition(); + $transitions = $this->getTransitions($transition, $field_name, $account, $force); } else { - if ($entity instanceof WorkflowTransitionInterface) { - $transition = $entity; - $transitions = $this->getTransitions($transition, $field_name, $account, $force); - } - elseif ($entity->{$field_name}->value) { - $transition = $entity->{$field_name}->first()->getTransition(); - $transitions = $this->getTransitions($transition, $field_name, $account, $force); - } - else { - // Empty field. Entity is created before enabling Workflow module. - $options = $workflow->getStates(); - return $options; - } + // Empty field. Entity is created before enabling Workflow module. + $options = $workflow->getStates(); + } - // Return the transitions (for better label()), with state ID. + // Return the transitions (for better label()), with state ID. + if (is_array($transitions)) { foreach ($transitions as $transition) { $to_sid = $transition->to_sid; // @see WorkflowConfigTransition::__toString(). $options[$to_sid] = $transition; } - // Save to entity-specific cache. - $cache[$entity_type_id][$entity_index][$force][$current_sid] = $options; } + // Save to entity-specific cache. + $cache[$entity_type_index][$entity_index][$force][$sid_index] = $options; + return $options; }