0

I know of two ways to theme views exposed filter forms in Views 3:

  • Copy the file views-exposed-form.tpl.php in sites/all/modules/views/theme to your theme folder and edit the HTML directly
  • Use hook_form_alter in your template file, for example:

.

   function yourtheme_form_alter(&$form, $form_state, $form_id) {
     switch ($form_id)  {
       case 'views_exposed_form':
         # do things to the array
       break;
     }
   } 

My problem is, I have different exposed filters on different views, and these approaches both hit all exposed filters equally. There's also nothing about exposed filters templates in the Theme Information on the views edit page.

How can I target these to one specific exposed filter on one specific view?

Views 3 with Better Exposed Filters (BEF) module, Drupal 7.

1

For the .tpl file

Views recognises files with names of the form views-exposed-form--view_id--display_id.tpl.php. You can also use less strict filenames of the form views-exposed-form--view_id.tpl.php or views-exposed-form--display_id.tpl.php.

While it's not addressed directly there, you can look up and copy-paste what these values should be by going to the view edit page, then Advanced > Theme > Information, then taking a suffix from an option under Display Output at the top.

For example, this is a view with machine name products, and I'm targeting its page display:

enter image description here

So the filename is views-exposed-form--products--page.tpl.php.

Other prefixes in the Display Output section can be used too, for example views-exposed-form--products.tpl.php would target all displays from the "products" view, including blocks or panel panes, while views-exposed-form--page.tpl.php would target all views pages.

For template.php

You can put a condition on the form's HTML id, which is generated along similar lines, like this:

   function yourtheme_form_alter(&$form, $form_state, $form_id) {
     switch ($form_id)  {
       case 'views_exposed_form':
         if($form['#id'] == 'views-exposed-form-viewid-displayid'){
             # do things to the array
         }
       break;
     }
   }

Or potentially even switch($form['#id']){} - but the original is written assuming you already have other uses of this hook based on $form_id for other forms.

Continuing with my previous example, it'd be views-exposed-form-products-page.

Note that this might need a different string if you've overwritten the form ID output anywhere, and there doesn't seem to be any way to target all displays short of something crude like if(strpos($form['#id'], 'views-exposed-form-products') !== false){ }

0

Should you need to render individual filter fields with custom html use the code below

<?php print $widgets['FIELD_ID']->label; ?> //for field Label <?php print $widgets['FIELD_ID']->widget; ?> //for field widget

Your FIELD_ID should be what you get in the bold html below :

<div id="edit-flight-way-wrapper" class="views-exposed-widget views-widget-filter-field_flight_way_value">

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.