How can change state programmatically?

Created on 5 October 2023, about 1 year ago
Updated 19 October 2023, about 1 year 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.71.5 2024