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 named "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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

berliner created an issue. See original summary.

berliner’s picture

Issue summary: View changes

berliner’s picture

Status: Active » Needs review

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 (Also raised that as a separate issue so that it's easier to review: #3527821: Tests are broken If that one get's merged, the MR for this issue here needs to be rebased.
  • 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.

ruslan piskarov made their first commit to this issue’s fork.

ruslan piskarov’s picture

Assigned: Unassigned » ruslan piskarov

  • ruslan piskarov committed 745d6324 on 8.x-1.x
    Issue #3526856 by berliner, ruslan piskarov: Redirect triggered by event...

ruslan piskarov changed the visibility of the branch 8.x-1.x to hidden.

ruslan piskarov’s picture

Status: Needs review » Fixed

Thank you @berliner.

claudiu.cristea’s picture

Services were removed in a minore release. That broke our site. We should not remove but deprecate.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.