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
- Drupal standard install
- Enable Replicate UI and Replicate Actions
- Enable Replicate for content under /admin/config/content/replicate
- Create and enable a custom module named "custom_replicate" with the following code
- Clear the cache (just in case)
- Create a node of type basic page
- Go to the edit page of that node and click on the "Replicate" tab
- Enter "Replicated title" into the "New label (English)" textfield and click the "Replicate" button
- 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
Issue fork replicate_actions-3526856
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
Comment #2
berliner commentedComment #4
berliner commentedI 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:
Comment #6
ruslan piskarovComment #9
ruslan piskarovThank you @berliner.
Comment #10
claudiu.cristeaServices were removed in a minore release. That broke our site. We should not remove but deprecate.