2

I want to get some value from session and put it into one of the exposed filter text fields of my view as default. Later the user can change the value and filter again.

Is there any way to do it? My session variable will be taken with:

<?php return $_SESSION['ip_geoloc']['location']['city']; ?>
  • "My session variable will be taken with:"? – Mohammad Ali Akbari Jun 29 '12 at 13:11
  • 1
    With what? is it a rebus? – Tyler Durden Jun 29 '12 at 13:12
  • Sorry, updated the question with the code...Actually it is not important. It can also be a simple variable – Sinan Erdem Jun 29 '12 at 13:19
1

You can use hook_form_alter and can give your session value as the default value of the exposed text field

The following code will help you

function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-XXXXXXXX") {

    $form['#after_build'][] = 'some_function';

 }
}

function some_function($form, &$form_state) {

 $default_value=$_SESSION['ip_geoloc']['location']['city'];
 $form['xxxxxx']['#default_value']=$default_value;

  return $form;
}
  • provide an example if possible, that help the user if he is new to Drupal :) – Mathankumar Oct 20 '12 at 5:35
  • You don't have to do it in #after_build. Just do $form['xxxxxx']['#default_value']=$default_value; directly in MODULENAME_form_alter() – milkovsky Mar 11 '15 at 11:29

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.