Download & Extend

Theming Exposed Filters Forms

Project:Views
Version:6.x-2.0-rc5
Component:exposed filters
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Hi,

I'm having trouble theming the Exposed Filter form. All I want to do is change the Submit button label to 'Search'. Following the Drupal 6 Override and Intercept idea, I've added the following to my theme:

function phptemplate_views_exposed_form($form) {
  $form['submit']['#value'] = t('Search');
  $output = drupal_render($form);
  return $output;
}

Upon clearing the cache, this changes the text of the submit button. However, I lose the labels on the filter widgets.

I've tried bringing the preprocess_views_exposed_form function into my theme and adding tpl.php for the function as well, but this breaks the Filter form (All widgets disappear).

Am I missing something? Any help would be greatly appreciated.

Cheers
Zap

Comments

#1

Yes, you are missing that views_exposed_form is already a template.

<?php
// $Id: views-exposed-form.tpl.php,v 1.4 2008/05/07 23:00:25 merlinofchaos Exp $
/**
* @file views-exposed-form.tpl.php
*
* This template handles the layout of the views exposed filter form.
*
* Variables available:
* - $widgets: An array of exposed form widgets. Each widget contains:
* - $widget->label: The visible label to print. May be optional.
* - $widget->operator: The operator for the widget. May be optional.
* - $widget->widget: The widget itself.
* - $button: The submit button for the form.
*
* @ingroup views_templates
*/
?>

<?php if (!empty($q)): ?>
  <?php
   
// This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
   
print $q;
 
?>

<?php endif; ?>
<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
          <label>
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>
    <div class="views-exposed-widget">
      <?php print $button ?>
    </div>
  </div>
</div>

The way to change that information most effectively is going to be to use hook_form_alter() on it.

#2

Status:active» closed (fixed)

Thanks Merlin!

#3

Embarrassingly enough even with views-exposed-form.tpl I can't figure out how to make simply changes to the forms such as changing the #size or #options variables. With Views 1 it was very easy, I just put in a piece of code like in the original post into my template.php and alter whatever variables I needed but things are a bit more complicated (and sophisticated) with Views 2 and I can't seem to figure out how to modify any form values AND keep the template. Looking into hook_form_alter() it seems to be a module function, is that to say that I need to create a module to easily modify the forms? Sorry if these are dumb questions, I hope someone can advise me.

#4

Status:closed (fixed)» postponed (maintainer needs more info)

#5

I also am having trouble here. Changing the $widget->label is obvious if you're just looking to change the label. And you can arrange the widgets easily.

But...for the actual individual fields within the exposed filter, which are locked into the "$widget->widget" entry--how would one get to the individual fields for theming? Is it possible to dissect this widget->widget data? What php code might be used?

#6

Same issue here if anyone figures it out.

#7

Does this help?

<?php
$view
= views_get_view('your_view_name');
 
$view->set_display('default');
 
$view->init_handlers();
 
$form_state = array(
   
'view' => $view,
   
'display' => $view->display_handler->display,
   
'method' => 'get',
   
'rerender' => TRUE,
   
'no_redirect' => TRUE,
  );
 
$output = drupal_build_form('views_exposed_form', $form_state);
  return
$output;
?>

#8

That's what I use to pop the view itself where I want it, but doesn't really get me into altering the exposed filter itself. (Which, I've since decided is pretty difficult to do as the data is being generated by calls to form api stuff.)

An example of something that one might want to change is the default max length of the list menu in the exposed filter--I believe it's 8 now (ie, any list with more than 8 entries will get scroll bars).

I finally settled on just substituting out the max length entry in the "widget" var via php string commands within the template file referenced above, but that's rather klugey, and was wondering if there's a method of actually addressing those kind of values more elegantly...

#9

I have similar problem with themeing exposed filter form, which I've explained here http://drupal.org/node/364859
Tried to solve it with hook form_alter but no luck yet.
(Already solved with help of merlinofchaos)

#10

Using form_alter to change the Apply button text

hook_form_alter is only usable in a module not a theme.

So if you don't have an project_extras module you'll want to create one.

And use the following snipplet,

function project_extras_form_alter(&$form, $form_state, $form_id) {
  if($form_state['view']->name == 'view_name') {
    $form['submit']['#value'] = t('Search');
  }
}

#11

I'm sure that theme:info is not documented! subscribe.

#12

I'm sure that theme:info is not documented!

Only if you don't, say, read the documentation.

#13

Joe

Any chance you could elaborate ? I have some experience theming template files and a little PHP, but I don't understand what project_extras is ?

Cheers

Dave

#14

It's a new module, like the views module. Any hook_alter code is most likely going in a module if you see it which means you need to save the code as ________.module. In this case he wrote the code to work as project_extras.module. You then need to make a project_extras.info (look inside a module directory for an example) and then enable it on your modules page.

#15

I think the hook_form_alter in a custom module gets called for every single form, whereas the views-exposed-form.tpl file only gets called when Views2 themes an exposed filter.

The hook_form_alter is easier (and works for me), but probably much less efficient. Right?

#16

Not at all, because you begin hook_form_alter from checking if($form_id == 'exposed_filter_form') {
and the rest of the code is used only when it is necessary.

#17

As example from CSS file,

#edit-keys {
width: 220px;
}

#edit-tid {
width: 220px;
}

solves the problem of Taxonomy Term and Search Exposed Filter box sizes. And the below code change at the end of views-exposed-form.tpl.php changes the "Apply" button to "Search" button.

Now, can someone help on this question: If we dont have labels in Exposed Filters, the "Apply" button shifts down into a lower line compared to the exposed filters. How can we correct this little issue. It looks bad as user experience. I tried to put some display:inline stuff, did not work. Any idea ? Thanks

<?php
// $Id: views-exposed-form.tpl.php,v 1.4 2008/05/07 23:00:25 merlinofchaos Exp $
/**
* @file views-exposed-form.tpl.php
*
* This template handles the layout of the views exposed filter form.
*
* Variables available:
* - $widgets: An array of exposed form widgets. Each widget contains:
* - $widget->label: The visible label to print. May be optional.
* - $widget->operator: The operator for the widget. May be optional.
* - $widget->widget: The widget itself.
* - $button: The submit button for the form.
*
* @ingroup views_templates
*/
?>

<?php if (!empty($q)): ?>
  <?php
   
// This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
   
print $q;
 
?>

<?php endif; ?>
<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
        <div class="exposed_filter_label">
          <label>
            <?php print $widget->label; ?>
          </label>
         </div>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>
    <div class="views-exposed-widget">
    <input type="submit" id="edit-submit" value="Search"  class="form-submit" />
    </div>
  </div>
</div>

#18

Question in #17 was:
---------------------------------------------------------------------------------------------------------------------------
If we dont have labels in Exposed Filters, the "Apply" button shifts down into a lower line compared to the exposed filters.
---------------------------------------------------------------------------------------------------------------------------------

Solved the question by getting rid of "<div class="views-exposed-widget">" below:

<div class="views-exposed-widget">
    <input type="submit" id="edit-submit" value="Search"  class="form-submit" />
</div>

----------------------------------------------------------------------------------------------------------------------------------

Now, the next question is:

How to insert text or label in between exposed filters ?

For example, if you have Search and Taxonomy Term Exposed filters, how can you achieve below:

Search "in" Taxonomy Term Apply
--------- ------------------- -------

where "in" can be the label for "Taxonomy Term" exposed filter, which is shown in the same line as filters and Apply button, instead ofbeing shown on top of "Taxonomy Term".

Thanks everybody for any solution to this question.

#19

Here we go, another question about Exposed Filter Theming:

If you have Taxonomy Term Exposed Filter with Autocomplete, and with CSS you try to increase the height of the box, say to 44px as below,

#edit-tid {
width: 220px;
height:44px;
}

then, the autocomplete "turning sign" (I dont know the official name) turns and turns and turns...

Purpose is to have a little larger exposed filter boxes in width & height.

So, how to stop that autocomplete sign , appearing ??

Thanks

#20

The issue with the button text change in #17 is that if someone presses the button, it reverts back to "Apply". I'll probably write a hook_form_alter into a tiny module to solve this minor issue.

Edit: Got a working module in place. Doesn't require any views-exposed-form.tpl.php overrides, though at the moment it's specific to my site and my view names. I'll keep working on advancing this to where it's general enough to contribute back.

#21

Thanks! Please do!

#22

I did notice one thing after making the tiny module. On the actual view editing UI, where there are forms (restore/remove display, add new display, etc.), the text of the button (say, "Click me") will show up next to those forms...since the form ID gets set after the "unset $form_state['#id']" commands somewhere inside the views module. I've seen them before in the code...I'll keep poking around under the hood though.

#23

Still poking away at this, I'll probably post a question to the issue queue to help get over the last hurdle, though it's probably releasable as is (got to apply for a CVS account first). I'm looking to only give the option override the exposed filters if the view has an exposed filter. So if a site has 50 views, but only one has an exposed filter, your settings page won't blow up =) It's also right now on a per-view basis, not a per-display basis. :(

#24

I found some code at http://stackoverflow.com/questions/451745/drupal-views2-exposed-form-how... :

function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {

  // Specify the Form ID
  if ($vars['form']['#id'] == 'views-exposed-form-jobs-search-page-1') {

    // Change the text on the submit button
    $vars['form']['submit']['#value'] = t('Search');

    // Rebuild the rendered version (submit button, rest remains unchanged)
    unset($vars['form']['submit']['#printed']);
    $vars['button'] = drupal_render($vars['form']['submit']);
  }
}

Remember to set your Form ID in the if statement. In my case I wanted it to affect all filters so I'm using :

function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {

    // Change the text on the submit button
    $vars['form']['submit']['#value'] = t('Search');

    // Rebuild the rendered version (submit button, rest remains unchanged)
    unset($vars['form']['submit']['#printed']);
    $vars['button'] = drupal_render($vars['form']['submit']);
}

#25

Using my method above does anyone know if there's a way to change the word < any > in a filters drop down box to < all >.

Here's a screen shot of what I'm talking about:
http://img27.imageshack.us/img27/5677/screenshotxch.png

#26

subscribing

#27

I just hacked the file modules/views/handlers/views_handler_filter.inc , around line 465
changing t('') to t(''). (String overrides module did not work in this case: I used String overrides module to change "Apply" button to "Search".

Cheers!, as always feels shame to hack the core !

#28

Hey SeanBannister,

Here is my solution for changing to All.
Newly added lines has shown as bold.
I hope this code helps.
======================================================================

function YOURTHEME_preprocess_views_exposed_form(&$vars, $hook) {

// Specify the Form ID
if ($vars['form']['#id'] =='views-exposed-form-jobs-search-page-1') {

// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Search');
$vars['form']['tid']['#options']['All'] = t('- All -');

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
unset($vars['form']['tid']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
$vars['widgets']['filter-tid']->widget = drupal_render($vars['form']['tid']);
}
}

#29

subscribing

#30

I just wrote up this blog post on how I used the pre-processing to include the taxonomy description in a views filter drop down. Might be of interest to people looking at this thread.

http://drupal.altate.ch/blog/theming-exposed-filtres-views-taxonomy-desc...

Cheers,

Neil

#31

This is my code to change the Filter box to say 'Search', the search words box to be an autocomplete dropdown (using finder) and to change the text to read 'All options'

<?php
function THEMENAME_preprocess_views_exposed_form(&$vars, $hook) {

 
// only alter the search view exposed filter form
 
if ($vars['form']['#id'] == 'views-exposed-form-search-default') {
       
       
   
// Change the text on the submit button to Search
   
$vars['form']['submit']['#value'] = t('Search');
    unset(
$vars['form']['submit']['#printed']);
   
$vars['button'] = drupal_render($vars['form']['submit']);
   

   
//Add autocomplete functionality to the keys textbox
   
$vars['form']['keys']['#autocomplete_path'] = 'finder_autocomplete/autocomplete/2/1';
    unset(
$vars['form']['keys']['#printed']);
   
$vars['widgets']['filter-keys']->widget = drupal_render($vars['form']['keys']);
   

   
//Change the <any> choice to All options
   
$vars['form']['type']['#options']['All'] = t('All options');
    unset(
$vars['form']['type']['#printed']);
   
$vars['widgets']['filter-type']->widget = drupal_render($vars['form']['type']);
   
  }
}
?>

I'm having some issues with the autocomplete though, not working after submitting, or on certain pages. Any suggestions welcome.

#32

I am trying to theme exposed TAXONOMY, but its hard to theme it as all the terms are on "same level" i would theme differntly parent from child but when i get just array with list of terms without any notice of hierarchy i cannot do that. I am looping trough terms with.

    foreach($vars['form']['tid_1']["#options"] as  $term_id => $value){
        $vars['form']['tid_1']["#options"][$term_id] = $value;

    }

how could i style differntly each term according to hierarchy?

#33

This is more like a metaquestion about this thread, and could be that I am completey overlooking something but the following comes to my mind:

Is it not like shooting a fly with an elephant-gun that one has to install a module (with a hook_form_alter or a string overrides module) just to change the text on a button of the standard (exposed filter) interface?
I would like to have all these interface texts as variables (with default values of course) which I can change on configuring every single view.

#34

hi, #31 is not working for me..

<?php
    $vars
['form']['type']['#options']['All'] = t('All options');
    unset(
$vars['form']['type']['#printed']);
   
$vars['widgets']['filter-type']->widget = drupal_render($vars['form']['type']);
?>

do I need to change something in the code? changing the submit button is working.. dunno why is not, thanx

#35

How could I use this to add words next to widget textboxes for instance, the add Max and Min for a Between Filter?

#36

@czeky
sites\all\modules\views\theme\views-exposed-form.tpl.php file
needs to be included in your theme folder (even unmodified)
before the function preprocess in your template takes effect

@bjraines your best bet would be to include
sites\all\modules\views\theme\views-exposed-form.tpl.php file
in your theme and include any labels you want

#37

I've changed the name of the button 'Apply' to my own by:
in template.php

function eska_preprocess_views_exposed_form(&$vars){
    $vars['button'] = str_replace('Apply', 'MY_NAME', $vars['button']);
    return $vars;
}

#38

can anyone provide assistance in the code needed to print each exposed filter separately in the template file.
the foreach doesn't allow me enough flexibility to design the exposed filter block.

    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
          <label>
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>

#39

Does anyone have any idea where i've gone wrong in the code?

Parse error: syntax error, unexpected '&', expecting ']' in

<?php
/**
* outputs counts of a given term
**/
function get_term_count_nodes ($tid){
   
   
// get the term object
   
$term_ob = taxonomy_get_term($tid);
   
   
//return only the description
   
return  $term_ob->count_nodes;
}

/**
* Preprocesses the filters for views
**/
function waffles_preprocess_views_exposed_form(&$vars, $hook) {


  
// First, change the text on the submit button from apply -> search
  
$vars['form']['submit']['#value'] = t('OK');

   
// add node count into drop down
      
    // foreach option in the drop down, get the TID and re-write to include the description
   
foreach($vars['form']['views_exposed_form']["#options"] as  $term_id => $value){
       
$vars[‘form']['views_exposed_form']["#options"][$term_id] = $value.' - '.get_term_count_nodes ($term_id);
    }

  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['
form']['submit']['#printed']);
   
unset($vars['form']['views_exposed_form']['#printed']);

   
// NB your filter-tid_2 will be different depending on which filter you are theming
 
$vars['button'] = drupal_render($vars['form']['submit']);
   
$vars['widgets']['filter-views_exposed_form']->widget = drupal_render($vars['form']['views_exposed_form']);
   
  }
}
?>

Thanks in advance!

Evelyn

#40

Your {} in the 2nd function do not appear to be balanced.

#41

$vars[‘form'][

This ` ' are not the same characters, you should use ' ' or " ";

#42

Thank you both for your help, it's greatly appreciated!!

Evelyn

#43

Status:postponed (maintainer needs more info)» fixed

It seems to be fixed

#44

Translate label value of : - Any - ,on optional single-select exposed filters (views exposed form dropdown)

http://druptips.com/node/18

#45

I used the following Code:

function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {

// Specify the Form ID
if ($vars['form']['#id'] == 'views-exposed-form-jobs-search-page-1') {

// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Search');

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
}
}

I changed my theme to my actual them of endless_xs. My view name is Job_search. Can anyone help me.

#46

Is your view name jobs search or job search?

Try:
if ($vars['form']['#id'] == 'views-exposed-form--job-search--page-1') {

Note there is two - instead of one - between form and job and between search and page.

#47

It is jobs_search. Does this makes a difference?

here is what I have:

function endless_xs_preprocess_views_exposed_form(&$vars, $hook) {

// Specify the Form ID
if ($vars['form']['#id'] == 'views-exposed-form--job-search--page-1'){

// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Search');

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
}
}

#48

yes it makes a difference because if it is job search the code should be:
if ($vars['form']['#id'] == 'views-exposed-form--job-search--page-1'){

if it is jobs search it should be:
if ($vars['form']['#id'] == 'views-exposed-form--jobs-search--page-1'){

#49

Still does not work. Here is what I put:

function endless_xs_preprocess_views_exposed_form(&$vars, $hook) {

// Specify the Form ID
if ($vars['form']['#id'] == 'views-exposed-form--job-search--page-1'){

// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Search');

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
}
}

endless_xs = theme name
job_search = view name

#50

To make even more interesting, how can we implement this to change the "Any" exposed forms using different text.

Example.

form 1 -- Any -- (change to for example: Type of Widget?)
form 2 -- Any -- (change to Color of Widget?)

then hit "Apply" or "Search" for example.

And I don't mean the title because it can set in Views exposed form. My plan is to remove title and replace it with that function I mention above. Any brilliant ideas?

#51

Can we use the views-exposed-form.tpl file for making the change happening, follow the steps:

  1. Copy the file, views-exposed-form.tp.php from sites/all/modules/views/theme and copy it to your theme sites/all/themes/YourTheme
  2. Open the file in an editor
  3. Find the line <?php print $button ?> (line number: 45)
  4. Change that line into <?php print str_replace('Apply','Search',$button); ?>
  5. Close, Clear Cache and check.

#52

this worked for me. Thank you!

#53

Status:fixed» active

I notice this theme file does not appear in the Theme Information section of a view?

How can we make it so?

#54

Status:active» fixed

Please don't hijack issues.

Fill a new issue if you have problems.

Are you still using Views 6.x-2.0-rc5? so...

#55

Status:fixed» active

is there a way to make certain fields in a filter available to only certain roles? I think this is a theming issue that is why I am asking as part of this discussion. If you recommend that I start a new issue, I will.

#56

Status:active» fixed

Changing the "Any" text still doesnt work for me.

function fas_preprocess_views_exposed_form(&$vars, $hook) {
//Change the choice to Choose
$vars['form']['type']['#options']['All'] = t('Choose');
unset($vars['form']['type']['#printed']);
$vars['widgets']['filter-type']->widget = drupal_render($vars['form']['type']);
}

I have included the sites\all\modules\views\theme\views-exposed-form.tpl.php file in my theme folder.Is there anything else that I need to change?

Thanks.

#57

If all you want to do is change the text on the button from "Apply" to "Search" I believe the simplest method is to use the String Overrides module.

http://drupal.org/project/stringoverrides

Works for me.

#58

@tustind: Simple but also problematic. The problem is every time the word "Apply" is used in any other portion of the site it will also be changed to "Search". So lets say you had another module that created some type of form and the users had to "Apply" the changes with the "Apply" button, using String Overrides that button would now say "Search".

#59

newbie here...

I am having trouble understanding how to implement preprocess_views_exposed_form() to alter

1) the maxlength of individual textfields
2) specific exposed select list options

here's what I have for texfields so far:

function mikli_preprocess_views_exposed_form(&$vars, $hook) {
  if ($vars['form']['#id'] == 'views-exposed-form-search-form-default') {
    $vars['form']['model']['#size'] = '10';
    $vars['form']['model']['#maxlength'] = '6';
  }
   $output = drupal_render($form);
   return $output;
}

thanks in advance to those who help!

#60

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

#61

@lmaharas

do a print_r($vars['form'])
or any other variable to see what is available to be modified
or look at the specific examples in this issue

#62

I've implemented the preprocess function outlined in #31 with success. Thanks burgs!

I've got one last formating issue with a checkbox filter which has a single option ('Yes'). I'd like to remove this label because I've specified a filter label of 'Editor Pick'.

I tried altering with:

$vars['form']['my_checkbox_item']['#options'] = '';

But, I quickly realized this creates all kinds of problems with the checkbox by removing the only option available.

I'm guessing that I'll have to modify the widget before it's rendered?

#63

More: Strip checkbox option label from exposed filter

Okay, so I've found the exposed filter template file (views-exposed-form.tpl.php) and I see how the $widget variable is printed within the file. My problem persists because the checkbox option label is already formated as:

$widget->widget

and looks like this when printed:

<div class="form-checkboxes "><div class="form-item" id="edit-pick-Yes-wrapper"> <label class="option" for="edit-pick-Yes"><input type="checkbox" name="pick[]" id="edit-pick-Yes" value="Yes" /> Yes</label> </div> </div>

Because there's only one option available for this checkbox, I want to delete the option label (in bold) since it's redundant to the widget label I specified while setting up the exposed filter:

...value="Yes" /> Yes</label>...

So, it looks like I need to modify $widget->widget before it is rendered. Anyone have a clue? Maybe there's an easy way to solve this in the view administration but I'm simply overlooking it? I'll report back if/when I find a solution.

#64

Status:closed (fixed)» active

Hello milehighlife,
First of all, thank you "burgs" for the great solution!

milehighlife, I'm not sure exactly if there is a solution to this in the admin view, but I assume that this code might help you in this:

/**
* Alter the exposed block form of ............. to remove the label "Yes" from the checkbox filter
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
//print_r($form);die;
switch ($form_id) {

case 'views_exposed_form' :
$form['submit']['#value'] = t('Search');
............................
............................
break;

}

  • You have to create/modify a module and add this code into it
  • Of course, custom_module is what you name the module
  • Beware that this will affect all the exposed filter forms in your drupal site unless you find a specific key of the form (hint: print the $form array)

I used this code in a booking website and it works awesome, though i used the arg(0) to know what is the current page (for example to remove check-out date from a tour package).

Hope that this will help :-)

#65

Do you all want to make a tutorial out of it?

#66

Why not? I guess many developers got stuck in it, as its not so obvious..

#67

rewriting all exposed forms somehow broke my hierarchical select, still had to filter it out by form id for this to work properly.

#68

#51, you're brilliant! It worked, easy and elegant -- thanks. And I have a multi-site setup so it will now work on all my sub-sites since I have one code base.

It's "hacking" Views, so I've added it to my log of things I need to do when updating.

#69

Subscribing,
greetings, Martijn

#70

Happy to hear that it worked!

#71

#39 This is really useful.
Thanks

#72

#51 works.

Thank you for posting.

Edit:

o.k. it stopped replacing the text for some reason. Also using the instruction stopped the filter from working properly in IE8. It would only search once, and then not search again without refreshing the browser page.

Update: the filter button works haphazardly in IE8, YMMV. Poifect. Just what I need. Grrr IE.

#73

Status:active» fixed

#51 is not brilliant in the way what it does. Additional renaming the button is in views3.

So this issue can be marked as fixed

#74

I was wrestling with translating View label value of : <Any>
#44 gave the clue, thanks joachim!

#75

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

#76

Thanks guys! I've used hook_form_alter for it with success:

<?php
/**
* Implementation of hook_form_alter().
*/
function MODULE_form_alter(&$form, $form_state, $form_id) {
   if(
$form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-tour-agents-page-1') {   
     
$form['distance']['search_units']['#type'] = 'hidden';
     
// ... manipulate more form fields
  
}
}
?>

#77

Status:closed (fixed)» active

Sorry to open this again but I've not yet found a solution for changing the <Any> label to All. Is it even possible?

I can change the label on the search button no worries. I have...

<?php
function expfilter2_preprocess_views_exposed_form(&$vars, $hook) {

// Specify the Form ID
if ($vars['form']['#id'] =='views-exposed-form-seat-markets-page-3') {

// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Filter');
$vars['form']['tid_1']['#options']['All'] = t('ALL');

// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
unset(
$vars['form']['tid_1']['#printed']);
$vars['button'] = drupal_render($vars['form']['submit']);
$vars['widgets']['filter-tid_1']->widget = drupal_render($vars['form']['tid_1']);
}
}
?>

I've been wondering if the setting in admin/build/views/tools/basic is overriding whatever I do.

Has anyone had success? Can you help me?

PS - this page really helped me: http://bjrtechnologies.com/completely-customize-views-exposed-filters

#78

Status:active» closed (fixed)

Ahh... I just found a way...

Credits to: http://snipplr.com/view/17307/hookformalter-for-views-exposed-filter-form/

<?php
/** Function for altering form below **/

function expfilter2_form_views_exposed_form_alter (&$form, $form_state) {

foreach(
$form as $key => &$value) {

if(isset(
$value['#options']['All'])) {

$label = ucwords(strtolower(str_replace('_', ' ', $key)));

$value['#options']['All'] = t('All');
}

$form['submit']['#value'] = t('Filter');
}

}
?>

I hope this helps someone.

#79

I have a similar request - I'm trying to change the exposed filter from a textfield to a dropdown. I don't really want to make a module just to hook_form_alter, but I found that you can access the exposed_widgets of the view template via the following code. However, even though I've changed the exposed widget, it's not printing my changes.

<?php
/**
* Overriding press-release aspects - requires presence of corresponding .tpl template file
* <a href="http://drupal.org/node/303586">http://drupal.org/node/303586</a>
* @param $vars
* @param $hook
*/
function MYTHEME_preprocess_views_view__press_releases(&$vars) {
   
$form = $vars['view']->exposed_widgets;
   
$newField = <<<NEWFIELD
<select name="created" id="edit-created">
    <option value="-1 Month" selected="selected">Last Month</option>
    <option value="-3 Month">Last 3 Months</option>
    <option value="-6 Month">Last 6 Months</option>
    <option value="-1 Year">Last Year</option>
    <option value="">All</option>
</select>
NEWFIELD;
   
   
$vars['view']->exposed_widgets = preg_replace('/<input(.*)name="created"(.*?)\/>/', $newField, $form);
}
?>

Most likely I'm just going after the wrong thing, meaning I should override the exposed_form or something else. If so let me know.

#80

+1 for 79, I have the same problem, want to show a textfield as dropdown (it's easy if they are allowed values, but seems tricky if they are not)

#81

#51 is too general and will only work for one Views exposed search. If you create another, or use another exposed Views search as some modules do (MERCI, for example) then changing views-exposed-form.tpl will apply the button name changes to those files too.

The same principle applies to using String Overrides.

#24 is a closer approach but the most correct way to theme a Views2 Exposed Form can be done at the template level. A detailed explanation is here:

http://stackoverflow.com/questions/5861519/drupal-views2-exposed-form

#83

Subscribing: Lots of great ideas here : )

#84

subscribing

#85

Subscribing

#86

Thanks, tribe_of_dan for sharing your solution. It worked well!

I'm kind of new to drupal and I'm not sure about one thing. I need to add some CSS to control the new "Apply" button's position on the screen (margin-top attribute). Should I do this in views.css or my theme's MYTHEMENAME.css file? I always get confused on what is "good" etiquette and what is not good. I think my theme, but not sure.

#87

Hi @hwasem. I would use your mythemename.css where possible. Just incase when you update Views your styles get overridden and lose your changes.

#88

@SeanBannister
Thanks Sean. This is exactly what I needed.

#89

Thanks tribe_of_dan...IT WORKS FOR ME



Now i am able to change the views exposed filter's "apply" button text to "Search" and also the "" option in the select list to "all". Here is my complete working code.

change "any" to "all" in the select list

to change "" to all the code is same of trib_of_dan but i would like to add a bit of explanation to it:

Place this code in your custom module



<?php
/** Function for altering form below **/

function MODULENAME_form_views_exposed_form_alter (&$form, $form_state) {

foreach(
$form as $key => &$value) {

if(isset(
$value['#options']['All'])) {

$label = ucwords(strtolower(str_replace('_', ' ', $key)));

$value['#options']['All'] = t('All');
}

$form['submit']['#value'] = t('Filter');
}

}
?>

change "apply" to "search":



All you need to do is to place this code in your theme templete.php file

<?php
function THEMENAME_preprocess_views_exposed_form(&$vars, $hook) {
   
// Change the text on the submit button
   
$vars['form']['submit']['#value'] = t('Search');

   
// Rebuild the rendered version (submit button, rest remains unchanged)
   
unset($vars['form']['submit']['#printed']);
   
$vars['button'] = drupal_render($vars['form']['submit']);
}

?>

#90

its all about how to theming in foreach ($widgets as $id => $widget)
$widget->widget
but if i wanna theming operator?
all operator stored in $widget->operator;

i found

<?php
function themename_form_alter(&$form, &$form_state, $form_id) {
// Change test to the name of your view
 
if ($form_id == 'views-exposed-form-catalog-page-1'  && $form_state['view']->name == 'test') {


   
// Change field_test_value_op to the identifier you specified
   
unset($form['field_test_value_op]['#options']['empty']);
   
unset($form['field_test_value_op]['#options']['not empty']);
 
}
}
?>

but it didnt work (with my view)
i need to unset some fields in operator select box

#91

I want to move the "Apply" and "Reset" buttons up to the top of the "exposed filter as a block" cuz my list is so long.

I was able to do it by editing views-exposed-form.tpl.php but that changes it for all exposed forms across the site.

How can I do it for just one form? I tried an "if form-id == my_form_id" in the .tpl file but it failed as an undefined variable.

Any suggestions, and should this be a new issue?

Thanks,
Tim

#92

Please follow the steps above and it should work for you:
http://drupal.org/node/320992#comment-3159692

#93

Thank you, this code worked without a hitch! One must remember to clear the cache (best done using Devel) though.