2

I have different roles for users of my Drupal 7 site. I have added a rule that whenever an authenticated user logs in they are directed to a node (say node/90). This works fine: both admin and authenticated users are redirected.

However I want that the admin be redirected to a different node say node/25. So I added a new rule and selected under condition that user role is administrator and in action also forced a redirect. However the admin still redirects to node/90. Why so and how can I achieve my use case?

As suggested by NikhilM, I have created a custom module for it and the .module file has

    <?php

function redirect_ad_help($path, $arg) {
  switch ($path) {
    case "admin/help#redirect_ad":
      return '<p>' . t("Redirect") . '</p>';
      break;
  }
}
function redirect_ad_user_login(&$edit, $account) {
 if($account->uid == 1)
  drupal_goto('node->97');
 else
  drupal_goto('node->69');
}

I also tried doing $edit['redirect'] = 'node/97'; instead of drupal_goto() but I am taken to the home page only and not to the desired node.

  • can you update your question with your latest module i.e after replacing node->97 with node/97 ? – GoodSp33d Mar 18 '13 at 13:12
  • @2-Stroker..have done that...thanks for your concern but its still not working...any suggestions – why Mar 18 '13 at 13:16
4

My guess would be that your rule weights are not as they should be. If your rule that matches the admin role fires first and your rule that matches all authenticated users fires second then admins will always be redirected to node/90.

Check on the weights. In your case, you want the more specific rules (the one for admins) to fire second. So you need to make its weight larger than the more general rule.

Does that make it work for you? I don't think you should need a custom module for this...

Also - disable the custom module while you're testing this out, to make sure the two are not interfering.

  • how can i change the weights for rules...even i thought of that option but was not getting how to do it – why Mar 19 '13 at 4:44
  • ok that did it..quite simple actually – why Mar 19 '13 at 6:50
1

You can do that with hook_user_login

function hook_user_login(&$edit, $account) {
 if($account->uid == 1)
  drupal_goto('node/25');
 else
  drupal_goto('node/90');
}

replace drupal_goto('node->97'); with drupal_goto('node/97');

Give it a try let me know the result.

  • I am not sure about the possibility to do this with Rules. – niksmac Mar 18 '13 at 11:39
  • ok..i have created a small custom module for this and in the .module file i have added: <?php function redirect_ad_help($path, $arg) { switch ($path) { case "admin/help#redirect_ad": return '<p>' . t("Redirect fro admin") . '</p>'; break; } } function redirect_ad_user_login(&$edit, $account) { global $user; if($user->uid == 1) drupal_goto('node->97'); }....but then the admin is taken to the home page – why Mar 18 '13 at 11:44
  • ...i added the module code in the question – why Mar 18 '13 at 11:50
  • 1
    replace drupal_goto('node->97'); with drupal_goto('node/97'); – niksmac Mar 18 '13 at 11:51
  • 1
    ...setting weights to rules did it....quite simple actually...no need to add any custom module – why Mar 19 '13 at 6:51
0

Even though the currently accepted answer may have helped to solve the issue in the question, I think it is not 100% accurate (not correct, maybe even a bit misleading). Moreover there is an alternative solution also. Read on for more details ...

About using Rules Weight

To indicate a "weight" for a custom rule, you use the field in the rules UI that looks like so:

enter image description here

Note what is written below that weight field, i.e "Order rules that react on the same event. Rules with a higher weight are evaluated after rules with less weight".

Let's apply this to what's suggested in the currently accepted answer:

  • There are 2 rules, which both seem to react to the same event (= "User has logged in").
  • By changing the weights of both rules, the rule with the lower weight is executed first, and the one with the higher weight afterwards. Quote from the accepted answer:

    ... you want the more specific rules (the one for admins) to fire second. So you need to make its weight larger ...

That's indeed "a" way to get it to work (to meet the specs as in the question). However, it does also mean that BOTH rules are executed, for anybody (not just admins ...) who performs a login:

  • The 1st rule for authenticated users gets executed for anybody who performs a login (note: so also for admins ...). That is, the redirect to node/90 gets executed.
  • Authenticated users that do not have the admin role, do not satisfy the Rules Condition, so the Rules Action of the 2nd rule for admin does not execute.
  • Authenticated users that do have the admin role, do satisfy the Rules Condition, so the Rules Action of the rule for admin does execute. That is, the redirect to node/25 gets executed also.

The result of this is that for a use that has the admin role, actually 2 redirects are performed: first to node/90 (as per the 1st rule), followed by a redirect to node/25 (as per the 2nd rule). For those who don't believe (or get) it: just add a Drupal action like "Show a message on the site" in both rules (for admins you'll get both messages).

Alternative to using Rules Weight

Apart from redundant Rules logic that gets executed (as explained above), using Rules Weights to get things to work (= implement the appropriate execution sequence) has some challenges, which I try to avoid whenever possible.

In this case (= scenario in the question), an alternative solution appears to be to use the Conditional Rules module. Have a look at this rule (in Rules export format) which uses this module, and which also implements the very same logic as described in the question:

{ "rules_redirect_each_role_to_separate_page" : {
    "LABEL" : "Redirect selected roles to selected pages",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_conditional", "rules" ],
    "ON" : { "user_login" : [] },
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "3" : "3" } } } },
            "DO" : [ { "redirect" : { "url" : "node\/25" } } ]
          },
          { "ELSE" : [ { "redirect" : { "url" : "node\/90" } } ] }
        ]
      }
    ]
  }
}

After you enabled the Conditional Rules module, just use the Rules UI to import the above rule in your own site, enable the rule, and see what happens ... No need for "Rules Weight", and only using a single rule (to see the entire Rules logic in a single screen when using the Rules UI). And for users that do have the admin role, there is no (repeat: no) redundant redirect to node/25.

What is more: imagine that you'd have to implement a redirect that depends on (say) 5 roles, so that you need to redirect to one of 5 pages. All it's take is to extend the IF/THEN/ELSE logic with 3 more appropriate redirects (instead of tuning the Rules Weights for 5 different rules ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.