D7: Page redirect after node creation doesn't work

Events happening in the community are now at Drupal community events on www.drupal.org.
mkelly's picture

I'm building an event calendar that accepts submissions from anonymous users - but before the submitted events are published, they have to be approved by a moderator.

I have a event content type, with publish set to default. Anonymous users have the right to create events, but can only view published content. The problem is that after an event is submitted, users get "You are not authorized to access this page.", which makes sense.

So, I've installed Rules (7.x-2.0-rc1) and created the following rule, which checks to see if the user has submitted content and has the anonymous role:

{ "rules_redirect_anonymous_users_after_submitting_event" : {
"LABEL" : "Redirect anonymous users after submitting event",
"PLUGIN" : "reaction rule",
"TAGS" : [ "anonymous", "event", "redirect" ],
"REQUIRES" : [ "rules" ],
"ON" : [ "node_insert" ],
"IF" : [
{ "user_has_role" : {
"account" : [ "site:current-user" ],
"roles" : { "value" : { "1" : "1" } }
}
}
],
"DO" : [ { "redirect" : { "url" : "\/thank-you", "force" : 0 } } ]
}
}

Unfortunately, it doesn't work. The user is not redirected and still sees the "not authorized" page. I've tried forcing the redirect as well, but to no effect.

Any thoughts? I've searched groups & the bug reports for Rules and haven't found an obvious solution. As far as I can tell, the approach above should work.

Comments

I am also having this problem

dippers's picture

I am also having this problem using $form_state['redirect'] in a submit handler. All redirect requests after node creation (or editing) are ignored. The problem is caused by node_form_submit() that sets its redirect to $form_state['redirect']=node/nid after node_save is called. All the hooks and rules are invoked by node_save so any chage to redirect in there are ignored and node_form_submit wins and always sends you to node/nid.

At the moment I cannot find a workaround as there is nowhere to break in with a user defined call between where 'redirect' is set and where it is actioned.

Programmatic workaround but not for Rules

dippers's picture

I have found a workaround for my situation of programmatically changing the redirect and node creation. Rather than add a submit handler to the form at $form['#submit'] add it to the button at $form['actions']['submit']['#submit']. This ensures it runs after node_form_submit and therefore can set redirect. Unfortunately this doesn't help with rules as these are invoked within node_form_submit and then ignored.

If you are familiar with the code you could create a new rule event, attach a custom submit handler as above and in the submit handler invoke the new rule event but this is a lot of work and it probably easier just to code the redirct directly.

The code would be someting like:

function MYMODULE_form_alter(&$form, &$form_state, $form_id){
  global $user;
  if ($user->uid == 0 && $form_id == 'MYNODETYPE_node_form' ){
    $form['actions']['submit']['#submit'][] = 'MYMODULE_do_redirect';
  }
}

function MYMODULE_do_redirect($form, &$form_state){
  $form_state['redirect'] = 'thank-you';
}

@Dippers Thanks this

marthinal's picture

@Dippers

Thanks this workaround works for me.

Cheers!

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: