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.

CommentFileSizeAuthor
#2 3171609-2.patch1.42 KBnicoloye

Comments

nicoloye created an issue. See original summary.

nicoloye’s picture

Status: Active » Needs review
StatusFileSize
new1.42 KB

Here is the patch

maxpah’s picture

Status: Needs review » Reviewed & tested by the community

  • nicoloye committed a074c85 on 8.x-1.x
    Issue #3171609 by nicoloye, MaxPah: Additional API to get workflow...
nicoloye’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.