Drupal Views2 Exposed Form how to change

23,123

Solution 1

If you want the drop-down to fire, I'd use JavaScript instead of hacking the module as Eaton suggests.

Basically, you can modify the text with hook_form_alter as Eaton suggests, then use in the same hook_form_alter, add a call to drupal_add_js with your custom JS which hides the button and submits the form on the onChange handler of the select drop-down. You want that submit button there for those 10% of users for whom the JS fails.

Solution 2

Both of the above are fine but I found out that altering the form might not always lead to desirable results, mainly because exposed filters are themed using a specifc theme template. The proper way of changing the theme would be to override the views-exposed-form.tpl file in your theme's folder. Bear in mind that this will apply to all exposed filter forms, to theme a specific one, you will need to use a different name for that filename, like:

views-exposed-form--TITLE--DISPLAY.tpl.php
views-exposed-form--TITLE.tpl.php

and some others, you can check the Theme: Information section of your views for template naming conventions.

Solution 3

This module provides an auto-submit among other things http://drupal.org/project/views_hacks

This module is great to improving exposed filters http://drupal.org/project/better_exposed_filters

Share:
23,123
frankish
Author by

frankish

Worked most of my career in Linux/UNIX environments with Perl, Java, Sybase. Worked as a Developer, QA Engineer and UNIX Systems Admininstrator I like to ask the good questions so all can benefit from the answers. Hard to beat the fastest guns in the west around here. ;-)

Updated on August 18, 2020

Comments

  • frankish
    frankish over 3 years

    I have a View with an exposed form . I am trying to a few things on it. Ideally I would like to have a dropdown that fires the form with no button. If that is not possible then I would like to have the button text something different than apply.

    I hacked it for now and change views_form in views.module but that does not seem like the right way to do it. I only have one exposed form right now, but what if I add more?

    Please see http://www.wiredvillage.ca/News for my example.

    I am poking around drupal.org and seeing others with the same problem but no solutions so far. Not sure where the best place to get Drupal help is.

    Here is the change I made so far:

    function views_exposed_form(&$form_state) {
      // Make sure that we validate because this form might be submitted
      // multiple times per page.
      $form_state['must_validate'] = TRUE;
      $view = &$form_state['view'];
      $display = &$form_state['display'];
      $form_state['input'] = $view->get_exposed_input();
      // Let form plugins know this is for exposed widgets.
      $form_state['exposed'] = TRUE;
      $form['#info'] = array();
      if (!variable_get('clean_url', FALSE)) {
        $form['q'] = array(
          '#type' => 'hidden',
          '#value' => $view->get_url(),
        );
      }
      // Go through each filter and let it generate its info.
      foreach ($view->filter as $id => $filter) {
        $view->filter[$id]->exposed_form($form, $form_state);
        if ($info = $view->filter[$id]->exposed_info()) {
          $form['#info']['filter-' . $id] = $info;
        }
      }
    
      // I CHANGED The VALUE OF THIS SUBMIT BUTTON TO GO
    
    
      $form['submit'] = array(
        '#name' => '', // prevent from showing up in $_GET.
        '#type' => 'submit',
        '#value' => t('go'),
      );
      $form['#action'] = url($view->get_url());
      $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
      $form['#id'] = views_css_safe('views_exposed_form-' . check_plain($view->name) . '-' . check_plain($display->id));
    //  $form['#attributes']['class'] = array('views-exposed-form');
      // If using AJAX, we need the form plugin.
      if ($view->use_ajax) {
        drupal_add_js('misc/jquery.form.js');
      }
      views_add_js('dependent');
      return $form;
    }