Drupal Views2 Exposed Form how to change

僤鯓⒐⒋嵵緔 提交于 2019-12-19 10:09:05

问题


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

回答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.




回答2:


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']);
  }
}



回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/451745/drupal-views2-exposed-form-how-to-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!