All articles

Replaying one Drupal Commerce order place subscriber on demand

When a Drupal Commerce order's place transition triggers a side effect, such as copying a paid Webform submission into contacts and syncing them to Salesforce, you can re-run just that side effect for one order by building the WorkflowTransitionEvent yourself and calling the one subscriber method directly. Put it behind an admin-only confirmation form on the submission page, and leave the order's state and every other subscriber alone.

This came up on a group of nonprofit membership sites that share one Drupal distribution. The tool was built by a teammate; I'm writing it up because the pattern is useful any time a state machine side effect needs a manual retry.

The setup: a paid webform that places an order

On these sites, some forms are paid. A visitor fills in a Webform, and the Commerce Webform Order module turns the submission into a Drupal Commerce order. When the order is paid and moves through the place transition, a custom event subscriber listening for commerce_order.place.post_transition does the real work: it reads the submission, creates or updates contact records for the people named in it, and pushes those to Salesforce through the Salesforce Suite module.

That works well until the data from one submission needs to be pushed again after payment, for example because a sync failed for a reason that has since been fixed. The order is already placed, so the transition will never fire again on its own. The data from that one submission is stuck.

Why not just dispatch the event again?

The obvious move is to dispatch commerce_order.place.post_transition a second time. The trouble is that you don't own every listener on that event. Commerce itself and contrib modules subscribe to order placement: they may send receipt emails, assign order numbers, adjust stock, or log analytics. Re-dispatching the event re-runs all of them, and some of those are not safe to repeat.

The other obvious move, pushing the order back to draft and placing it again, is worse. It changes real order history, fires the pre-transition and post-transition events for both moves, and leaves an audit trail that no longer matches what the customer actually did.

What you actually want is narrower: run exactly one handler, with exactly the input it would have received, for exactly one order.

How the team's tool replays one handler

The team's fix was a confirmation form, shown as a local task tab on the webform submission page. When an administrator confirms it, the form:

  1. loads the order related to the submission,
  2. gets the order's workflow and its place transition,
  3. builds a WorkflowTransitionEvent by hand from those, and
  4. passes it to the one subscriber method that copies data and syncs contacts.

Because the subscriber method takes the event object, it doesn't know or care whether the event came from the state machine or from the form. Nothing is dispatched, so no other listener runs, and the order's state never changes.

A fresh, minimal version of the submit handler looks like this. The service id and method name are placeholders for your own subscriber:

use Drupal\state_machine\Event\WorkflowTransitionEvent;

public function submitForm(array &$form, FormStateInterface $form_state): void {
  $order = $this->orderForSubmission($this->submission);
  if (!$order) {
    $this->messenger()->addError($this->t('No order found for this submission.'));
    return;
  }

  $state = $order->getState();
  $workflow = $state->getWorkflow();
  $transition = $workflow->getTransition('place');

  // The last argument is the state field's name.
  $event = new WorkflowTransitionEvent($transition, $workflow, $order, 'state');

  // Call the one handler directly; dispatching would re-run every listener.
  $this->contactSyncSubscriber->onOrderPlace($event);

  $this->messenger()->addStatus($this->t('Contact sync re-run for order @id.', [
    '@id' => $order->id(),
  ]));
}

How orderForSubmission() finds the order depends on how your site links the two; typically the order item carries a reference back to the submission, so a query on order items is enough.

Keeping it admin-only

A tool that re-runs a Salesforce sync is not something you want an editor clicking by accident. The route uses a _custom_access callback rather than a plain permission, so it can check two things at once: that the submission belongs to the right webform, and that the user has the administrator role.

example_sync.replay_place:
  path: '/admin/structure/webform/manage/{webform}/submission/{webform_submission}/replay-place'
  defaults:
    _form: '\Drupal\example_sync\Form\ReplayPlaceForm'
    _title: 'Re-run contact sync'
  requirements:
    _custom_access: '\Drupal\example_sync\Form\ReplayPlaceForm::access'
public static function access(WebformInterface $webform, AccountInterface $account): AccessResultInterface {
  return AccessResult::allowedIf(
    $webform->id() === 'membership_application'
    && in_array('administrator', $account->getRoles(), TRUE)
  )->addCacheableDependency($webform)->cachePerUser();
}

The same check hides the tab on every other webform, so the action only appears where it means something.

Is the handler safe to run twice?

Calling a handler directly only helps if the handler can cope with being run a second time for the same order. That's the real design question, and it lives in the subscriber, not the form.

For contact sync, two cases matter. One person can appear in a submission under several roles (say, both the primary contact and the billing contact), so the handler must match existing contacts and update them rather than creating duplicates. And a contact the first run already updated should end up in the same state after the second run, not with doubled values or a second set of related records. If the handler has a step that is not naturally repeatable, such as sending a welcome email, that step should check whether it has already happened, or be split out so the replay doesn't call it.

What I like about this approach is that it forces that question into the open. Once there's a button that re-runs the handler, you have to decide what "run again" means for each side effect, which is a question the original code could quietly avoid.

Key takeaways

  • To re-run one Drupal Commerce order transition side effect, build a WorkflowTransitionEvent from the order's workflow and call that subscriber's method directly.
  • Re-dispatching the transition event re-runs every listener, including Commerce and contrib ones that are not safe to repeat.
  • Moving an order back to draft to place it again rewrites real order history and should be avoided.
  • A _custom_access callback lets one route check both the webform and the user's role, which keeps a replay tool where only administrators can reach it.
  • A replay button is only safe if the handler is idempotent, so check how it treats existing and already-updated records before exposing it.

Written by Artem Shymko, a senior software engineer in Belgium working on Drupal, .NET and LLM features. Get in touch if you are working on something similar.