How to redirect user to a specific page after they login if they belong to a certain role? - drupal

We have certain users in our member list that have a role "vendor" attached to them. All such members are to be redirected to a certain page upon login. How can this be accomplished?

There is more than one way to skin this cat...
This is my preferred Drupal 7 method:
function hook_user_login(&$edit, $account) {
$edit['redirect'] = 'node/123';
}

For Drupal 7
Action --> admin/config/system/actions - Redirect to URL
then enable your trigger module
Trigger --> /admin/structure/trigger/node
if your are trying to login redirect just follow this(select user tab in the page)
go to --> admin/structure/trigger/user
then
Trigger: After a user has logged in
choose an action -->Redirect to URL and assign.
Then clear the cache.
It will work for you!

You can define actions and triggers in Drupal:
Action(admin/settings/actions)
- Redirect to a specific page
Trigger (admin/build/trigger/user)
- After user has logged in
Try this.
EDIT (see comments):
Create a small module to check on a user's login process what role he has and then redirect if neccesary.
drupal_goto => redirect-function in drupal
hook_user =>triggers on user operations
And for the user's roles:
GLOBAL $user;
$roles = $user->roles;
$vendor = in_array('vendor', $roles);
$vendor then holds a true/false value will decide to redirect or not.
If you don't know how to do this, just post here and I'll write the module for you. But this would be a good practice for writing future drupa modules for you perhaps. :)

There are 2 ways in DRUPAL 7
1) Using action and trigger
see this http://drupal.org/node/298506
2)if using custom module
function YOURMODULE_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset' || variable_get('login_destination_immediate_redirect', FALSE)) {
if(in_array('THE-ROLE-WANTED-TO-REDIRECT',$account->roles)):
drupal_goto('PATH');
else: drupal_goto('user/'.$account->uid);
endif;
}
}

You can use Rules
Events: User has logged in.
Condition: User has role
Actions: Page redirect

There are modules that do this (besides Trigger+Actions), such as LoginDestination: http://drupal.org/project/login_destination. This Drupal forum post has a bit more info about it as well.

following condition for hook_user
if($op =='login') drupal_goto("your path");

This can be achieved by using a combination of content access and login toboggan modules. You'll be able to restrict pages and prompt user to login to access them.

First set the conditions in form preprocess (for example I want to redirect only users that logged in using the form at node page)
function YOURMODULE_form_user_login_alter(&$form, &$form_state, $form_id)
{
$pathArguments = explode('/', current_path());
if (count($pathArguments) == 2 && $pathArguments[0] === 'node' && is_numeric($pathArguments[1])) {
$form_state['nodepath'] = current_path();
}
}
than define redirect:
function YOURMODULE_user_login(&$edit, $account)
{
if (isset($edit['nodepath']) && !empty($edit['nodepath'])) {
drupal_goto($edit['nodepath']);
}
}

Related

Silverstripe - Admin Login Form / Member Login Form

I am building a Silverstripe site that allow users to sign up to something. I have a few pages that in the CMS I set the page visibility to "Logged-in users" this is great but the default action is to redirect to /Security/Login. Is there a simple way of change the redirect for normal pages to goto etc /Account/Login and leave the default /Security/Login for CMS users?
Thanks
With the help of another plugin, I used this bit of code
public function onBeforeSecurityLogin()
{
$backUrl = $this->owner->getRequest()->getVar('BackURL');
if (!strstr($backUrl, '/admin/')) {
if (Controller::curr()->class != 'Account') {
$link = 'account/login' . '?BackURL=' . urlencode($backUrl);
return $this->owner->redirect($link);
}
}
}
And I also extended the Security Class to create my own handler and form for logins
You can set ?BackURL=/Account/Login in your links, or in the Session.
Alternatively set the config variable, E.g:
Security:
login_url: Account/Login

How to redirect user after registration on Drupal 8?

How to redirect user to a particular page after the user is successfully registered into the site?
Here drupal_goto() does not work. We need to work with the form redirection. How can this be achieved?
// include the Url namespace in your custom module
use Drupal\Core\Url;
// Add CUSTOM SUBMIT HANDLER FOR REGISTRATION
$form['actions']['submit']['#submit'][] = '__tmp_registration_submit';
function __tmp_registration_submit(&$form, FormStateInterface $form_state) {
// set relative internal path
$redirect_path = "\user\uid\payment-history";
$url = url::fromUserInput($redirect_path);
// set redirect
$form_state->setRedirectUrl($url);
}
Maybe a bit heavy, but you could also use the Rules module.

How can I redirect a Drupal user after they create new content

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

How to logout and display message after login under certain condition?

Given a Drupal 7 website I want to customize log in behavior: When a users logs in I want to check if they are in a blacklist. If they are in that list I want them to be automatically logged out and told about the reason they are being kicked out. So In one of the custom modules of this drupal I have add the following hook:
function mymodule_user_login(&$edit, $account) {
if(blacklist(&$edit, $account)) {
drupal_goto("/user/logout/");
drupal_set_message('Acces denied','error');
}
}
However, my code doesn't work. What I'm doing wrong? I'm sure the hook is executed because I checked this using a watchdog. However, I also discovered the hook only gets executed if admin is the user who is logging in.
Are there any alternatives (maybe using Context module)? Any suggestions would be aprreciated!
Thanks!
Your code does not work because drupal_goto() calls drupal_exit() so the rest of your script does not get executed.
What you are trying to do will not work because user_logout() itself calls drupal_goto() to the front page.
You can however add an extra validation callback in the user login form so you can prevent users from logging in altogether.
<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_login_block':
$form['#validate'][] = 'mymodule_custom_user_validation',
break;
}
}
function mymodule_custom_user_valudation($form, &$form_state) {
if (// Add your blacklist conditons here. ) {
form_set_error('', t('Your account is blacklisted, therefore you cannot login to this account'))
}
}
?>
If your interested in using the Login Destination Module, then you could create some type of rule to hook into your blacklist check and then redirect that user to the "user/logout" path. Let me know if you have any questions.
What exactly is your "blacklist()" function doing?

Drupal: redirecting users with specific role to another page

How can I redirect only users with a specific role to the admin page when they login ?
Well, you need to get the global $user variable. And then check if the user has the desired role, (in this case I assumed the desired role is 'authenticated user'), and the current page is not the admin page (so you don't get a redirection problem), then redirect him to the admin page, or a page of your choice inside drupal_goto('admin');
There you go:
<?php
global $user;
if(in_array('authenticated user', $user->roles) && arg(0) != 'admin') {
drupal_goto('admin');
}
?>

Resources