0

i have preprocess function

function THEME_preprocess_views_view(&$vars) {
    global $user;
    $view = $vars['view'];

    if ($view->name == 'questions_by_category' && $view->current_display == 'page') {
        $vars['header'] = '<div>' . l('Ask your question', 'node/add/question') . '</div>';
        if($user->uid == 0) {
            //this code not working
            if($_GET['q'] == 'node/add/question') {
                $path = drupal_get_path();
                drupal_goto('user/login', $path);
            }
        }
    }

i need to redirect anonym user to login page when he click on the link node/add/question, i also tried with arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'question' but this wont work.

The code inside if($user->uid == 0) { } works fine, i can redirect, but i need to redirect on click only. Also i need to redirect user back on the page after login, and this code :

$path = drupal_get_path();
drupal_goto('user/login', $path); 

seems not working too.

0

ok i found solution, i just changed link for anonym user

if ($view->name == 'questions_by_category' && $view->current_display == 'page') {
        if ($user->uid) {
            $vars['header'] = '<div>' . l('Ask your question', 'node/add/question') . '</div>';
        }
        if ($user->uid == 0) {
            $vars['header'] = '<div>' . l('Ask your question', 'user/login') . '</div>';
        }
    }
| improve this answer | |
  • 1
    To redirect your users back to the page after login, you can use "?destination=page" parameter in url, like this: l('Ask your question', 'user/login', array('query' => array('destination' => 'node/add/question'))) – ermannob Jan 14 '13 at 19:15

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.