6

I'm trying to insert a form element with hook_form_alter(), and then rearrange the form elements so that the new form item appears at a particular point amongst the existing exposed filters.

I've tried adding #weight of 0 to all existing items and then #weight of -1 to the new element, just to see if I can have any influence on the form item order at all, however I don't seem to be able to affect it.

EDIT: In addition I'm getting kinda weird behaviour regarding the container of the Form element. I'm adding my form element as a new array element at the root of $form, however it is be rendered inside the element <div class="views-exposed-widget views-submit-button"> rather than getting it's own container, which seems like either a bug or I really am doing something wrong!

5

Take a look at comments #3, #7 and #9 in http://drupal.org/node/893392

#3 (dereine):

The problem is that there is some theming on exposed form. You would have to override it, sadly.

#7 (manatwo):

Yes, all of it is handled in views-exposed-form.tpl.php, which you can override in your theme to print out your fields manually. IIRC, the extra fields are gonna be in the $button variable in that template, so you might want to do some preprocessing in template.php

#9 (PadreHomer):

I was able to alter the order without overriding the template. I am using Views 3, but I think this will work in Views 2 as well.

Alter the order of the filters in $form['#info'] in your form_alter and this should alter the order the widgets print out in the template without having to do any custom templating.

  • Ahh this is a little unfortunate, I really enjoy nice clean hooks and overrides, shame something as respected as Views kinda messes with this! Thanks for the info though :) – DanH Aug 30 '11 at 4:07
  • Ahh having looked into it deeper now, comment #9 is exactly what I was looking for :) – DanH Aug 31 '11 at 4:03
  • @DanH: I also tried with #9, but no success. its not at all changing its weight. FYI: I have enabled better exposed filter in views and I have secondary filter as well. – Suraj Oct 17 at 6:16
2

A small update on this (as I have just updated http://drupal.org/node/893392 too), if someone faces similar problem in the future, as I just did myself, trying to place my new exposed form item in-between "widgets" generated automatically by the view - the solution is to turn that new form item into a widget, and then re-order widget array in the $form['#info'].

In my case I was adding new community form item into Views exposed form, and then wanted it to be displayed as a second element in the form, right after filter-title generated by the view:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-mw-group-list-page-1') {
    // Add new form item.
    $form['community'] = array(
      '#type' => 'radios',
      '#title' => t('Community'),
      '#options' => $options_array,
      '#default_value' => $default_value,
    );
    // Create widget from new form item and place it as a second element.
    $new_form_info = array(
      'filter-title' => $form['#info']['filter-title'],
      'filter-community' => array('value' => 'community'),
    );
    $form['#info'] = array_merge($new_form_info, array_diff_assoc($form['#info'], $new_form_info));
  }
}

Obviously the code responsible for re-ordering $form['#info'] will look different depending on where the new form item should be displayed.

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.