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

后端 未结 9 1202
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 04:26

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 ac

9条回答
  •  鱼传尺愫
    2020-12-11 05:01

    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']);
        }
    }
    

提交回复
热议问题