Drupal 7 alter views exposed form

Categorías: 

Etiquetas: 

Objective

Alter form fields of a previously created View called 'Example view'. The system name will be 'example-view'.

Method 1

  • In your module, call hook form_alter with the id from the view exposed form
/**
 * hook form_alter
*/
function my_module_form_alter(&$form, &$form_state, $form_id)
{ 
 
  // Alter view page with system name "example-view" to add a placeholder in the title field
  if($form['#id'] == "views-exposed-form-example-view-page"){
 
    if (isset($form['title'])) {                        
      $form['title']['#size'] = 25;
      $form['title']['#attributes'] = array('placeholder' => array(t('Search by Name')));
    }
  }  
}

Method 2

  • In your module, call hook views_exposed_form_alter
/**
 * hook views_exposed_form_alter
*/
function my_module_views_exposed_form_alter(&$form, $form_state) {
  // change date format example from a field called 'date_filter'
  $form['date_filter']['value']['#date_format'] = 'd-m-Y';
}