Form alter trick: Form alter views exposed forms

Posted by: 
Dominique De Cooman

Our example: we try to set the default value for a filter on a certain week.

I tried to set the default value of your exposed filter in:

<?php
$form
['week']['#default_value'];
?>

Didnt work.
In get forms you need to set the $form_state['input']['week']. Also you need to make sure the value isnt submitted yet so check on $_GET['week'] to get the actual value.

Here is a code example

<?php
/**
 * Implementation of hook_form_alter()
 */
function boek_top100_form_alter(&$form, &$form_state$form_id) {
  
//Sets the default top 100 week ie. the latest
  
if ($form_id == 'views_exposed_form' && arg(0) == 'top100') {
    
$result db_result(db_query("SELECT week FROM {boek_top100} ORDER BY id DESC LIMIT 1"));

    if (
$_GET['week'] == '') {
      
$form_state['input']['week'] = $result;
    }
  }
}
?>

Add new comment