1. Start creating a node
  2. Preview node
  3. Return to the node creation form
  4. Hit 'Cancel'

You are returned to the node preview rather than the content overview or whatever path you'd configured.

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

mrogers created an issue. See original summary.

ad0z’s picture

It's module behavior due to the redirect back to the HTTP referer, when there is no form_state redirect, destination parameter or entity edit form. When you go back from preview page, HTTP referer is set to preview page.
I agree it's unexpected behavior and should be considered as a bug.
I think we should keep initial HTTP referer when form is displayed first time and redirect user there.
I've prepared patch which implement it. It was quite hard to store this information in tempstore.private with unique key as entity uuid change when node save submit is pressed and HTTP referer is not expected one anymore.

ad0z’s picture

Status: Active » Needs review
mrogers’s picture

Amazing, patch is working flawlessly on our site. Thank you @ad0z !

heddn’s picture

Status: Needs review » Needs work
+++ b/cancel_button.module
@@ -131,7 +131,13 @@ function cancel_button_form_alter(&$form, FormStateInterface $form_state) {
+        $uuid = $form_state->get('form_display') ? $form_state->get('form_display')->uuid() : NULL;

Can we add some code comments about what this is doing and what it is protecting us against? This is pretty dense code and some explanation will go a long way to help future selves know what it is doing.

shivam_tiwari’s picture

Assigned: Unassigned » shivam_tiwari

I am working on this.

shivam_tiwari’s picture

Assigned: shivam_tiwari » Unassigned

@heddn I checked this issue and provided patch is also, but I am getting same issue on content add form. I tested in Drupal 11.0.9.
I followed following steps:
1. Enabled module and added redirect for node form.
2. When I am going to add article, on add article node form I get content listing page URL on the cancel button. (/node/add/article)
3. After filling out the form go to the preview page and get back, this cancel button URL is changed to preview page URL.
Can you please suggest here what will be the correct URL at this time? It will be the (/node/add/article) or the URL that I set on configurations.
cc @Maintainers

boddy’s picture

Assigned: Unassigned » boddy
boddy’s picture

in Drupal 10.4.6 with the provided patch the link of Cancel button gets stuck if the user dismisses adding the new node.
Steps to reproduce:
* install and enable Admin Toolbar, Admin Toolbar Extra Tools modules;
* from the any page e.g. '/admin/content' open the dropdown menu Content>Add content>Article of Admin Toolbar Extra;
* check the link of Cancel button, it leads to '/admin/content';
* do not save content, just go to the '/node/add' and hit the Article link;
* check the link of Cancel button, it still leads to '/admin/content' instead of '/node/add';

Working on it.

boddy’s picture

Unset the referrer if it is a return from the Preview and skip saving the referrer to tempstore if any. Get the saved destination. This method allows you to update the destination of the "Cancel" link if the add form is next opened from other pages, e.g. from the Admin Toolbar Extra Tools

boddy’s picture

Status: Needs work » Needs review

Please review the merge request.

boddy’s picture

Hi there, please review the merge request, thanks!

boddy’s picture

Assigned: boddy » Unassigned
heddn’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Can we add tests for this scenario?

boddy’s picture

Added the test.
It does the 5 steps: visits /admin/content, clicks "Add content" (node/add/article), fills the title and hits "Preview", finds and clicks "Back to content editing" to return to the add form, then finds and clicks "Cancel" and asserts the final address is exactly admin/content.
Against the current (fixed) cancel_button.module the test passes.

  • boddy committed dc80cd16 on 8.x-1.x
    Issue #3401610 by ad0z, boddy: Cancel returns user to node preview
    
boddy’s picture

Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.