0

I'm creating an action to trigger on a rule when a user is logged in. I have a field of the user that is a term reference.

I would like to redirect to the url field of the chosen term. This is how I get the chosen url:

$tid = $user->field_redirect_field['und'][0]['tid'];
$taxonomy = taxonomy_term_load($tid);
$url = $taxonomy->field_url['und'][0]['safe_value'];

I receive a perfect url like stocklijst/gezocht (this works ok). Now I would like to redirect to that url. I've tried this:

drupal_goto($url);

But it doesn't redirect. It always goes to the front-page...

UPDATE:

This is what I do now:

I have a rule (When user logged in) that triggers a custom action "Redirect to user url". It's a custom action like this:

function redirectonlogin_rules_action_info() {
    $actions = array(
        'redirectonlogin_redirect_login' => array(
            'label' => t('Redirect on login'),
            'group' => t('My custom actions'),
            'parameter' => array(
                // curent user
                'user' => array(
                    'type' => 'user',
                    'label' => t('User'),
                    'description' => t('The current logged in user.'),
                    'save' => FALSE,
                ),
            ),
        ),
    );
    return $actions;
}

function redirectonlogin_redirect_login($user) {
    // REDIRECT TO USER REDIRECT FIELD
    $tid = $user->field_redirect_field['und'][0]['tid'];
    $taxonomy = taxonomy_term_load($tid);
    $url = $taxonomy->field_url['und'][0]['safe_value'];

    unset($_GET['destination']);
    drupal_static_reset('drupal_get_destination');
    drupal_goto($url);
}

The user can select a url in a select list. These are taxonomy terms... .

This works perfectly. But it takes some time, could there be a cleaner way and faster way of doing this?

2

2 Answers 2

1

Ideally, you could use hook_user_login(&edit, $account). This hook runs after a user logs in and isn't dependent on the Rules module:

function redirectonlogin_redirect_user_login(&$edit, $account) {
  $term = taxonomy_term_load($account->field_redirect_field['und'][0]['tid']);

  // These 2 lines may not be required depending on your set up.
  unset($_GET['destination']);
  drupal_static_reset('drupal_get_destination');

  $edit['redirect'] = $term->field_url['und'][0]['safe_value'];
}

But if something is interfering, like a drupal_goto() in another module that forces a redirect to the normal landing, you might need to replace:

$edit['redirect'] = $term->field_url['und'][0]['safe_value'];

with:

drupal_goto($term->field_url['und'][0]['safe_value']);

drupal_goto() forces a redirect and will ignore anything that comes after it in the page request.

2

You could use hook_form_alter(), In this hook we could redirect with $form['redirect'] = $url;

2
  • The login form? But I need data from the user ... And he's not logged in yet. It's not possible I think?
    – nielsv
    Commented May 30, 2014 at 15:02
  • Hi, you could load user value from user input. From user name, user mail
    – Leopathu
    Commented May 30, 2014 at 15:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.