4

I have added flag links to content pages for anonymous user. When user clicks on that link, the user should be redirected to user login page but after successful login the user should be redirect to last visited page.

I have used this code of a previously posted thread:

<?php
  global $user;
  if(!$user->uid) {
  // $flag: bookmark or make_feature, $action: flag, $content_id: $nid.....
  $flag_link = flag_flag_link($flag, $action, $content_id);

  $flag_link = flag_flag_link($flag, $action, $content_id);
  $flag_url = $flag_link['href'];

  //echo implode("/",arg());
    if(isset($flag_link['query']['token'])) {

      $flag_url .= '?'.$flag_link['query']['token'];

    }
  print l(t('SAVE '), 'user/login' , array('query'=> array('destination' => $flag_url)));
  print l(t('LIST'), 'user/login' , array('query'=> array('destination' => $flag_url)));
  } else {
    print render($content['links']['flag']);
  }

By using this code, anonymous user can redirect to login page but not to last visited page.

4
  • Why don't you try to use link type to javascript toggle in setting of that flag type? I will work on ajax no need to redirect.
    – vgoradiya
    Sep 29, 2015 at 12:09
  • 1
    possible duplicate of How to redirect user "back two pages" after login - the older questions counts login page as "first one back", so the intent is the same.
    – Mołot
    Sep 29, 2015 at 12:17
  • My issue is when anonymous user click on the flag link he/she redirect to login page->last visted page +flagged content must be save there
    – user52318
    Sep 29, 2015 at 12:24
  • finally got the anwer apply slight change in this line use current_path() instead of using $flag_url in:print l(t('SAVE '), 'user/login' , array('query'=> array('destination' => $flag_url))
    – user52318
    Sep 29, 2015 at 12:35

2 Answers 2

1

According to one of the comments (from OPer) below the question, it is possible to make the code excerpt in the question work in this case via a slight change in this line of that code:

print l(t('SAVE '), 'user/login' , array('query'=> array('destination' => $flag_url))

It should use current_path() instead of $flag_url, so that the corrected line looks like so:

print l(t('SAVE '), 'user/login' , array('query'=> array('destination' => current_path()))

Note: This answer is (just) a polished version of one of the comments (from OPer) below the question. But by posting it as an actual answer, I'd hope it'll be more easy to digest (and not so hard to find the answer somewhere in the comments). Hopefully also more easy to "find"?

2

The following implementation of hook_user_login() will override the redirect path set by user_login_submit().

As with the default behavior, setting the destination in the query string overrides this redirect.

<?php

/**
 * Implements hook_user_login().
 */
function mymodule_user_login(&$edit, $account) {
  $edit['redirect'] = $_GET['q'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.