views:

8451

answers:

3

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;
}
A: 

You should be able to use hook_form_alter() (http://api.drupal.org/api/function/hook_form_alter) to change the form as it's built, modifying the fields in question when that particular view is being displayed. You can nuke the submit button, add a #theme function that calls the drupal_add_js() function, and so on.

As long as the GET params come in the way views expect them, everything will work fine -- it was designed that way to allow bookmarking of pages with exposed filter settings, etc. The important part is to make sure you're doing the form mangling in your own module's hook_form_alter() function, so that it won't make other views driven stuff choke.

Eaton
+3  A: 

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.

David Wees
Yeah, David's suggesting is a sound one. For clean degradation on non-JS clients, leave the button there but remove or hide it with JS. You can still use hook_form_alter() to add in the theme function that calls drupal_add_js, however.
Eaton
Do you have modify the select drop-down to add a onChange handler?
Brian G
Only if you remove the button, otherwise there's no way for Drupal to know the view's exposed filter has been updated. You don't need to submit the page though, if you are loading the results via Ajax, you might be able to hook into that, but some kind of onChange handler is going to be necessary.
David Wees
+3  A: 

Or, you could use a preprocess function to alter the form even before it is build. I wanted to change the text on the button, so I did this:

function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {

  // only alter the jobs search exposed filter form
  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']);
  }
}

related questions