2

For some reason I want to alter the value of a select list in an exposed form of views .

I've added this code to the template.php and also Clear the cache :

function mytheme_preprocess_views_exposed_form(&$vars) {
  $myviewID = 'views-exposed-form-ouvrage-page';
    if ($vars['form']['#id'] == $myviewID){
      foreach($vars['form']['field_categorie_tid']["#options"] as $option_term){
        $optionterm = $optionterm."test";
       }
     }
}

After this with devel themer I can see the changes that I've mad but when I clear the cache the changes dosen't take in the select list

am I missing something ? I'm new with preprocess function

1 Answer 1

2

In your foreach loop, $option_term is a copy of the current variable returned from the iteration. If you want to change the original variable (i.e. what's in $vars['form']['field_categorie_tid']['#options'][index]) then you need to pass it by reference:

foreach($vars['form']['field_categorie_tid']["#options"] as &$option_term){
    $option_term .= 'test';
}

Or refer to the array member explicitly like so:

foreach($vars['form']['field_categorie_tid']["#options"] as $key => $option_term){
    $vars['form']['field_categorie_tid']["#options"][$key] .= 'test';
}

I generally prefer the second method as passing variables by reference in this way can sometimes cause some strange behaviour.

You also had a slight code error, the variable is called $option_term in your loop declaration but $optionterm in the loop body.

Just FYI, template preprocess functions are really not the best place for this sort of code. You would be far better off using hook_form_alter().

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.