Problem/Motivation

When deleting an entity via a contextual link, the user will be taken to a 404 page after confirming the deletion due to the destination parameter added by contextual.js

This is also impossible to override due to #2988181: contextual.js overrides any destination query param set earlier

Steps to reproduce

Delete a node via a contextual link on the node page.

Proposed resolution

Explore options in #22

Remaining tasks

  1. Test
  2. Fix

Comments

acbramley created an issue. See original summary.

shailja179’s picture

I am working on this issue.

shailja179’s picture

Status: Active » Needs review
StatusFileSize
new2.08 KB

Try with the given patch. It should work now.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

tanubansal’s picture

Tested patch #3 on 9.1, its working fine
Able to delete contextual link without 404

abhijith s’s picture

Applied patch #3 and it works fine. The 'page not found' error is showing after deleting content is gone.
Adding recordings

before patch:
Only local images are allowed.

after patch:
Only local images are allowed.

abhijith s’s picture

Status: Needs review » Reviewed & tested by the community
abhijith s’s picture

StatusFileSize
new579.22 KB
new601.76 KB
acbramley’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Needs reroll

Patch is failing to apply and needs tests.

ramya balasubramanian’s picture

Status: Needs work » Needs review
StatusFileSize
new2.09 KB

Reroll the patch for 9.2.

Status: Needs review » Needs work

The last submitted patch, 10: 3164482-10.patch, failed testing. View results

ramya balasubramanian’s picture

Status: Needs work » Needs review

Moving this to needs review, since that was a random test failure. Again ran the test cases and it was passed.

acbramley’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

Still needs tests...

ramya balasubramanian’s picture

Status: Needs work » Needs review
StatusFileSize
new3.61 KB

Added test. Please have a look and let me know if there are any issues.

psf_’s picture

Status: Needs review » Needs work
diff --git a/core/modules/contextual/js/contextual.es6.js b/core/modules/contextual/js/contextual.es6.js
index 12f1405a80..77aa210d02 100644
--- a/core/modules/contextual/js/contextual.es6.js
+++ b/core/modules/contextual/js/contextual.es6.js
@@ -98,11 +98,14 @@
       .prepend(Drupal.theme('contextualTrigger'));
 
     // Set the destination parameter on each of the contextual links.
-    const destination = `destination=${Drupal.encodePath(
-      Drupal.url(drupalSettings.path.currentPath),
-    )}`;
     $contextual.find('.contextual-links a').each(function () {
       const url = this.getAttribute('href');
+      if (url.indexOf("delete") >= 0 && url.indexOf("node" ) >= 0){
+        const destination = 'destination=' + Drupal.encodePath(drupalSettings.path.baseUrl);
+      }
+      else {
+        const destination = 'destination=' + Drupal.encodePath(Drupal.url(drupalSettings.path.currentPath));
+      }
       const glue = url.indexOf('?') === -1 ? '?' : '&';
       this.setAttribute('href', url + glue + destination);
     });

This patch only will work with Node entities, but we have the problem with Group entities. Must it be more generic?

psf_’s picture

The correct way may be always to send to the base path in 'Delete' links?

Replacing:

if (url.indexOf("delete") >= 0 && url.indexOf("node" ) >= 0){
  const destination = 'destination=' + Drupal.encodePath(drupalSettings.path.baseUrl);
}
else {
  const destination = 'destination=' + Drupal.encodePath(Drupal.url(drupalSettings.path.currentPath));
}

With:

if (url.indexOf("delete") >= 0){
  const destination = 'destination=' + Drupal.encodePath(drupalSettings.path.baseUrl);
}
acbramley’s picture

+++ b/core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php
@@ -217,6 +217,29 @@ public function testTokenProtection() {
+  public function testContextualLinksNodeDestination() {

This test has whitespace and indentation issues. It's also not actually testing the fix at the moment.

The best approach is to upload a test-only patch alongside your patch with the fix AND the test in order to prove that your test is testing the fix correctly.

anmolgoyal74’s picture

Status: Needs work » Needs review
StatusFileSize
new1.36 KB
new3.29 KB
new2.33 KB

Updated CS and indentation issues.

acbramley’s picture

Status: Needs review » Needs work
+++ b/core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php
@@ -217,6 +217,28 @@ public function testTokenProtection() {
+  public function testContextualLinksNodeDestination() {

This still isn't asserting anything about the bug or fix. It needs to be a javascript test since these changes are in JS.

geek-merlin’s picture

Hmm, hardcoding the 'delete' is a brittle kitten-killer. We might instead look for, say, a "no-destination" query parameter in the link.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mstrelan’s picture

Issue tags: +#pnx-sprint

I don't think it's the right approach to simply change the destination parameter of all contextual links with "node" and "delete" in the URL. It might be better to remove the destination for delete links if the current route matches any of the link templates for the entity in context. The destination parameter should remain if the entity is embedded on another page. It's also worth considering what happens with the cancel button on the confirm form, it points to the destination parameter with a fallback to the entity's canonical route.

An alternative approach would be to leave the destination parameter in place and move this logic to the submit handler of the confirm form. If the destination will result in a 404 due to the entity being deleted then the destination should be removed.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Agree with what @mstrelan said in #22, those seem like approaches worth exploring

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Issue summary: View changes
Issue tags: -Needs tests +Bug Smash Initiative

Came up for triage in BSI, removing all patches and needs tests tag as we need to start from scratch here.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.