Using Rules to redirect to OG homepage

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
NonProfit's picture

Hi, on my D7 sitem every user will be a member of one Organic Group.

How can Rules redirect user to group page on login?

Thanks!

Comments

sorry what you mean by

narcisgirona's picture

sorry what you mean by organic group?

Try to add a new rule that triggers whenever the user login.

admin/config/workflow/rules/reaction/add

and add: react on event: user has loged in

Hope this helps

Group page

NonProfit's picture

narcisgirona, Thanks for your response.

Organic Groups is a module which allows you to group users. Check out the project page and the list of supporting modules for more details.

I'm able to redirect a user on login, my issue is finding the group page for the user.

Thanks!

Got it

NonProfit's picture

OK, got it.

Events: User has logged in

Conditions: Check role

Actions:
Execute custom PHP code:

global $user;
$uid = $user->uid;
$result = db_query ( "SELECT * FROM {og_membership}
WHERE etid = :uid
and entity_type = 'user'
order by gid DESC", array (':uid' => $uid ) );

// get the results
foreach ( $result as $row ) {
// Do stuff with $record, which is an object
$gid = $row->gid;
break; //get the latest one by breaking here
}
$_SESSION['ses_user_group_gid'] = $gid;

Page redirect:
<?php print "partners/".$_SESSION['ses_user_group_gid'];  ?>

Cool! But write a mini-module

Itangalo's picture

Sweet that the problem is solved!
But you should really consider writing a mini module with this action, instead of using "execute custom PHP code". Why?

  • The custom PHP code uses the eval function, which is slow. (It actually parses your code twice!)
  • With errors in the custom PHP code, you may get really weird bugs on your site, and very little chance of tracing them.
  • The custom PHP code is stored in the database, which means that if someone manages to get the proper site permissions – they can totally wreck your site by changing the PHP code. (Code in a module is much more safely stored.)
  • Writing a Rules action is just a tiny bit more complex than writing custom PHP code.
  • Writing a Rules action will make you understand how Rules and Drupal works better.
  • Writing a Rules action is fun!

If I have convinced you, have a look at this screencast – it shows you how to write a Rules action. http://dev.nodeone.se/en/a-simple-action
Have fun!
//Johan Falk, Sweden

You can us Data Selectors to redirect to a group

rickyd1's picture

I know this thread is a little old, but I believe rules can do this without the custom php.

I had a similar issue. For me, ALL users will be in a group.

I used data selectors to check if they are a member of the group. I used group 0 because they could be a member of more than one group. I then use data selector on page redirect. I redirect them to the url for group 0.

I have tested it with members in more than one group, members in one group and members not in a group. It has been working as expected.

I have exported and included the rule below.

{ "rules_log_into_group" : {
    "LABEL" : "Log into group",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "OG" ],
    "REQUIRES" : [ "og", "rules" ],
    "ON" : [ "user_login" ],
    "IF" : [
      { "og_user_in_group" : {
          "account" : [ "account" ],
          "group" : [ "account:og-membership:0:group" ],
          "states" : { "value" : { "1" : "1" } }
        }
      }
    ],
    "DO" : [ { "redirect" : { "url" : [ "account:og-membership:0:group:url" ] } } ]
  }
}

Hope this helps some people.

Thanks!

NonProfit's picture

Thanks Itangalo & rickyd1!

Confusion

enap's picture

Neither of these solutions are working for me...

The PHP evaluation made more sense to me so I tried that first.

Action: User has logged in
Condition: Check role (authenticated user)
Actions: The PHP snippet & slightly modified page redirect (I use 'schools' instead of 'partners' in group URL aliases).

Login as a member of a group (that has posted content to a group also), I just see /node (default). Rule doesn't seem to fire. Have tried with NO conditions also.

Tried the exported rule but the replacement patterns you are using don't seem to exist (I'm using og-7.x-2.x-dev) and there doesn't seem to be any similar ones.

Had various attempts at it, could never get the rule to fire. Cleared caches etc. No change.

Any ideas?

EDIT: Got it working with the PHP snippet. Was more due to the platform I was setting it up on. PHP snippet works out of the box.

This solution does work!

SloanInnovations's picture

I know this thread is a little old, but I believe rules can do this without the custom php.

I had a similar issue. For me, ALL users will be in a group.

I used data selectors to check if they are a member of the group. I used group 0 because they could be a member of more than one group. I then use data selector on page redirect. I redirect them to the url for group 0.

I have tested it with members in more than one group, members in one group and members not in a group. It has been working as expected.

**********Small Tweek***********

On the conditions section under Groups, click the button that says switch to the direct input method and then select the proper group you need.

Thanks

jeuelc's picture

The solution presented by NonProfit is working out of the box. No editing needed.

Thanks a lot mate.
Cheers!

Using with Group instead of OG

Joel MMCC's picture

In our case, we use the Group module instead of Organic Groups. I did a Login Redirect rule with the aid of Conditional Rules to check for the number of Groups that the User is a member of.

If only one Group, it takes the user straight to that Group’s home page (group/gid”). If a member of other than one Group, it takes the user to his/her User Groups page (“user/uid/group”) which lists the Groups that the User is a member of. The gid (actually, the Group’s home page’s whole URL) and uid are obtained through normal Group means:

{ "rules_login_redirect_to_group" : {
    "LABEL" : "Login Redirect to Group",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "NOT entity_is_of_type" : { "entity" : [ "account" ], "type" : "group_membership" } },
      { "user_has_role" : {
          "account" : [ "account" ],
          "roles" : { "value" : { "1" : "1", "2" : "2" } }, /* This may differ on your site! Choose the Roles to apply this Redirect to. */
          "operation" : "OR"
        }
      }
    ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "group_membership",
            "property" : "user",
            "value" : [ "account" ],
            "limit" : "2" /* For performance — we only need to know if there’s more than one, not fetch them all. */
          },
          "PROVIDE" : { "entity_fetched" : { "group_memberships_fetched" : "Fetched Group Memberships" } }
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "list_count_is" : { "list" : [ "group-memberships-fetched" ], "value" : "1" } },
            "DO" : [
              { "redirect" : { "url" : [ "group-memberships-fetched:0:group:url" ], "force" : "0" } }
            ]
          },
          { "ELSE" : [
              { "redirect" : { "url" : "user\/[account:uid]\/group", "force" : "0" } }
            ]
          }
        ]
      }
    ]
  }
}

Remove the /* comments */ before Importing this. You may want to edit the Roles section as it may cause the import to fail if you have fewer than two Roles defined, given this example. Don’t forget to Edit that post-Import and set the Roles you actually want to apply or not to apply this Rule to (such as not having it trigger on Administrators).

Something similar could probably be done with OG.

{

Liliplanet's picture

{ "rules_on_login_redirect_to_group" : {
    "LABEL" : "On Login Redirect to Group",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "og", "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "og_user_in_group" : {
          "account" : [ "account" ],
          "group" : [ "site:current-user:og-user-node:0" ],
          "states" : { "value" : { "1" : "1" } }
        }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : [ "account:og-user-node:0:url" ], "destination" : "1" } }
    ]
  }
}

Rules

Group organizers

Group categories

Categories

Group notifications

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