How can change state programmatically?

Created on 5 October 2023, 9 months ago
Updated 19 October 2023, 8 months ago

Hi,

I have a form with the statuses, "Payment (pay)" and "Paid (paid)". if the status is "Payment", the user sees a link to the payment, when clicked, it is transferred to the registration page. The submission id is attached to the order. If the payment status is Successful, I get access to the submission, with the data:

"workflow" => array:8 [β–Ό
  "changed placemark" => "169641511"
  "changed user" => "1"
  "log_admin" => ""
  "log_public" => "Must be paid"
  "transition" => ""
  "workflow_state" => "pay"
  "workflow_state_label" => "Payment"
  "workflow_state_previous" => ""
]

My code:

$data = $webform_submission->getData();
$data['workflow']['workflow_state'] = 'paid';
$data['workflow']['workflow_state_label'] = 'Paid';

$webform_submission->setElementData('data', $data);
$webform_submission->save();

How do I programmatically change the "workflow_state"?

With regards.

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Documentation

Created by

πŸ‡·πŸ‡ΊRussia antiden

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @antiden
  • The module is designed to use transitions as part of the core workflows module, so you'd need to transition the state from one to the other, rather than simply change the workflow state. So, you'd have a transition from "unpaid" to "paid", say "mark as paid", and you'd run that on the submission at the point of payment.

    Then, there is a function on the workflows manager service to help do that:

        $workflowsManager = Drupal::service('webform_workflows_element.manager');
        $element_id = 'workflow';
        $transition_id = 'mark_as_paid';
        $log_message = 'Paid in order ' . $order->getOrderNumber();
        $success = $workflowsManager->runTransition($webform_submission, $element_id, $transition_id, $log_message);
        if ($success) {
          $webform_submission->save();
        }
    
  • πŸ‡·πŸ‡ΊRussia antiden

    Thanks for your answer, but I get an error. Transition name: paid (from Pay to Paid). My code:

    $workflowsManager = Drupal::service('webform_workflows_element.manager');
    $element_id = 'workflow';
    $transition_id = 'paid';
    $log_message = 'Paid in order ' . $order->getOrderNumber();
    $success = $workflowsManager->runTransition($webform_submission, $element_id, $transition_id, $log_message);
    if ($success) {
        $webform_submission->save();
    }
    

    Error: Call to undefined method Drupal\webform_workflows_element\Service\WebformWorkflowsManager::runTransition()...

  • Ah, it only exists in the dev version. If you're using the current alpha, you may need to use runTransitionOnElementValue and then look at the logic in the dev version for runTransition to do the transition on the element: https://git.drupalcode.org/project/webform_workflows_element/-/blob/1.0....

  • πŸ‡·πŸ‡ΊRussia antiden

    Yay, installed dev version, and runTransition() it worked. Big thx!

Production build 0.69.0 2024