Drupal - Add placeholder/help text to Views exposed filter?

You can do this with preprocess functions. Here's a tutorial on doing this for the default search box; you can do views exposed filters in a similar way.

You may also be able to use the Compact Forms module to do this, though I don't know if it is compatible with Views exposed filters.

Also, there's a module for showing helper text below a field: Better Exposed Filters

To set it up, simply enable the module and then navigate to your view edit page. Under Exposed Form, click Basic next to Exposed form style and change it to Better Exposed Filters. Then adjust as necessary.

However, this module will not automatically hide the text upon input.


I've been searching for that and could add placeholder with that way:

  1. Open your theme's template.php file (if not exist create one).
  2. Add the code below to your template.php file:

(Read the notes below the code.)

function mmsearchresults_form_alter(&$form, &$form_state, $form_id){
  if($form_id == "views_exposed_form"){
    dsm($form); // print $form array on the top of the page
    if (isset($form['title'])) {
            $form['title']['#attributes'] = array('placeholder' => array(t('Gemeente...')));
    }
  }
}


Note 1: If the form_alter function is already exist, do not add it again, just copy and paste the if($form_id=...) part.

Note 2: Change the 'title' whatever element you want to add placeholder.

Note 3: If you want not to display label, you could do this by css (display: none) or by adding unset($form['title']['#title']); inside of the code above.

Tags:

Views