Problem/Motivation
This issue is based on a support request on the Drupal France association slack where somebody asked for a way to catch the current step of a given entity.
Proposed resolution
Forked the current getWorkflowByEntity() method in the WorkflowManager service to allow retrieving of all workflow instances regarding a given entity.
API changes
Introducing a getAllWorksflowByEntity() method that performs the same logic getWorkflowByEntity() performed but return all the workflow instances instead of just the first one.
Code example
public function getMyEntityUrl() {
$my_entity = $this->getMyEntity();
if ($my_entity instanceof MyEntityInterface) {
/** @var \Drupal\forms_steps\Entity\FormsSteps $formsSteps */
$forms_steps = $this->entityTypeManager()->getStorage('forms_steps')->load('my_entity_type');
$workflows = \Drupal::service('forms_steps.workflow.manager')->getAllWorksflowByEntity($my_entity);
$last_workflow = end($workflows);
if (empty($last_workflow) || !($last_workflow instanceof WorkflowInterface)) {
return NULL;
}
$step_name = $last_workflow->step->value;
$instance_id = $last_workflow->instance_id->value;
$step = $forms_steps->getSteps()[$step_name];
return Url::fromUri("internal:" . RouteHelper::getStepUrl(
$step,
$instance_id
));
}
return NULL;
}
Thanks to MaxPah, Davy Duthoit, Mupsi and Jérôme Dehorter for their involvement.
Comments
Comment #2
nicoloye commentedHere is the patch
Comment #3
maxpahComment #5
nicoloye commented