Redirect triggered by event handler is not always working well with Replicate UI

Created on 27 May 2025, about 1 month ago

Problem/Motivation

When using Replicate UI to trigger a replication from the UI and doing some custom form altering to add additional submit handlers to do something with the replicated entity, the redirect event handler from this module can trigger the redirect before the other submit handlers have finished.
In my case, I was programmatically replicating entities attached to paragraphs in the replicated entity, and that apparently took too long (but really just 2 seconds or something like that) and was interrupted by the redirect. It took my a while to understand what happened, because it left no trails in the log either.

Steps to reproduce

  1. Drupal standard install
  2. Enable Replicate UI and Replicate Actions
  3. Enable Replicate for content under /admin/config/content/replicate
  4. Create and enable a custom module custom_replicate with the following code
  5. Clear the cache (just in case)
  6. Create a node of type basic page
  7. Go to the edit page of that node and click on the "Replicate" tab
  8. Enter "Replicated title" into the "New label (English)" textfield and click the "Replicate" button
  9. Confirm that you are redirected to the edit form of the replicated node and that there is a number after the title "Replicated title", but that this number is not 25

Code for the module file of a custom module called "Custom replicate":

<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * @file
 * Primary module hooks for Custom replicate module.
 */

 /**
  * Implements hook_form_alter();
  */
function custom_replicate_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, '_replicate_form')) {
    $form['actions']['submit']['#submit'][] = 'custom_replicate_custom_form_submit';
  }
}

/**
  * Custom submit handler for the replicate form.
  *
  * Add numbers to the replicated entity. If not interrupted, the number 25
  * should be appended to the replicated entity's title.
  */
function custom_replicate_custom_form_submit(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\node\NodeInterface $replicated_entity */
  $replicated_entity = $form_state->get('replicated_entity');
  $original_title = $replicated_entity->label();
  $count = 0;
  while ($count < 25) {
    $count++;
    $replicated_entity->setTitle($original_title . ' ' . $count);
    $replicated_entity->save();
  };

}

Proposed resolution

Don't do the redirect in an event subscriber, but rather set the redirect url on the form state in a form alter hook.

Remaining tasks

Confirm the issue and create a PR.

User interface changes

none

API changes

none

Data model changes

none

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany berliner

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024