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

Created on 27 May 2025, 10 days 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

  • Issue created by @berliner
  • Pipeline finished with Success
    10 days ago
    Total: 177s
    #507892
  • Pipeline finished with Success
    10 days ago
    Total: 219s
    #507904
  • Pipeline finished with Success
    10 days ago
    Total: 148s
    #507912
  • Pipeline finished with Success
    10 days ago
    Total: 329s
    #507914
  • 🇩🇪Germany berliner

    I have created a test-only branch where I added a test that should fail due to the described issue.

    The other branch with the MR should fix that test. What it does:

    • Make some fixes for the functional tests so that they are actually passing
    • Remove the redirect part from the event subscriber and rename the event subscriber to better reflect what it does now, which is assuring that replicated entities are unpublished
    • Add a form alter service that alters the replicate confirm forms and adds a submit handler to the replicate button. That submit handler then sets the form state redirect to the node edit url.
Production build 0.71.5 2024