I have found Rules Links to be essential in my current project because, if using Rules, it is easy to redirect a user to a specific URL after a link has been triggered and the rule has been executed. One major irritation however was that I could not set a destination parameter to redirect to a URL if a user clicked on 'cancel' when using the confirmation form option. They would always be taken to the front page.

Although I managed a work around using two sets of rules links, the first pointing to the url of the second and then getting Rules to set a redirect, it was very hacky confusing and a little too complicated and I don't really need to go into it here being as we now have another solution - the purpose for my post.

MorbiD in this issue stated half way down the page:

It was also important to add a ?destination=node/[node:nid] parameter to the redirect path, otherwise clicking "Cancel" on the rules_link confirmation page sent the user to the home page instead of back to the node they edited.

However this doesn't work because, if the action is cancelled on the confirmation page, then the rule is never triggered and Drupal never gets to know about the destination parameter.

Also, you can't just add a destination parameter to the path field in the Rules Link edit page. This is because Rules Link appends the URL of the content type the rule link is attached to whatever is set in this field. So, for example, if you enter 'my-rules-link-path?destination=somewhere-next' in this field, on the normal link setting Rules Link renders the full link as 'http://my-site/my-rules-link-path?destination=somewhere-next/645' (645 being the nid if attached to a node type) which of course doesn't work.

So to resolve this issue I have made some code changes to rules_link.admin.inc and rules_link.module which in effect add a destination field to the Rules Link edit form and makes sure the destination is added to URLs AFTER Rules Link adds the content type reference and the token.

This is my first ever alteration to a module and I'm unfamiliar yet with patching issues - something I need to work on. However here is what I have done to resolve the issue in the short term. Perhaps Rules Link maintainers will review it and make a proper patch from it. In the mean time if it helps others here's what I've changed:

diff --git a/sites/all/modules/contrib/rules_link/rules_link.admin.inc b/sites/all/modules/contrib/rules_link/rules_link.admin.inc
index aee03ad..3129eae 100644
--- a/sites/all/modules/contrib/rules_link/rules_link.admin.inc
+++ b/sites/all/modules/contrib/rules_link/rules_link.admin.inc
@@ -69,6 +69,7 @@ class RulesLinkUIController extends EntityDefaultUIController {
     $rules_link->settings['bundles'] = $values['bundles'];
     $rules_link->settings['entity_link'] = isset($values['entity_link']) ? $values['entity_link'] : '';
     $rules_link->settings['view_mode'] = isset($values['view_mode']) ? $values['view_mode'] : '';
+    $rules_link->settings['destination'] = $values['destination'];
     if ($rules_link->settings['link_type'] == 'confirm') {
       $rules_link->settings['confirm_question'] = $values['question'];
       $rules_link->settings['confirm_description'] = $values['description'];
@@ -286,6 +287,13 @@ function rules_link_form($form, &$form_state, $rules_link, $op = 'edit') {
     '#type' => 'textfield',
     '#field_prefix' => $base_url . '/',
   );
+  
+  $form['destination'] = array(
+    '#title' => t('Destination'),
+    '#description' => t('(Optional) Internal destination parameter. Useful for redirecting if user cancels when using the confirmation page. e.g node/256'),
+    '#default_value' => isset($rules_link->settings['destination']) ? $rules_link->settings['destination'] : '',
+    '#type' => 'textfield',
+  );
 
   if ($op == 'add') {
     RulesPluginUI::formDefaults($form, $form_state);
diff --git a/sites/all/modules/contrib/rules_link/rules_link.module b/sites/all/modules/contrib/rules_link/rules_link.module
index afbcb52..bd85bd6 100644
--- a/sites/all/modules/contrib/rules_link/rules_link.module
+++ b/sites/all/modules/contrib/rules_link/rules_link.module
@@ -189,7 +189,9 @@ function rules_link_render_link($rules_link, $entity_id, $destination = NULL, $p
       $path .= '/' . implode('/', $parameters);
     }
     $path .= $rules_link->settings['link_type'] == 'confirm' ? '' : '/' . rules_link_get_token($entity_id);
-
+    if ($rules_link->settings['link_type'] == 'confirm') {
+      $path .= '?destination=' . $rules_link->settings['destination'];
+    }
     $link = array(
       '#title' => $rules_link->getSettingTranslation('text'),
       '#href' => $path,

You'll also have to remember not to update your Rules Link module via Drupal update until this issue is patched on the latest release. Or else you'll have to re-apply the above changes. I usually put "DO NOT UPDATE" as well as a link to the issue on Drupal in my .info file description to remind me.

Cheers

Comments

drunkencelt created an issue. See original summary.

drunkencelt’s picture

Issue summary: View changes