4

I'm trying to add more elements to the exposed views filter form but am running into an odd problem that is confusing the heck out of me.

I'm using a hook_form_alter call in a custom module to add new select drop downs. This works great, except that they get rendered out as part of the submit element.

function my_module_form_alter(&$form, &$form_state, $form_id) {
  $form['test'] = array(
    '#type' => 'select',
    '#title' => 'New Filter',
    '#options' => array('value 1', 'value 2')
  );
}

Rendered it looks like this:

<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <div class="views-exposed-widget views-widget-filter-field_node_status_value_many_to_one">
      <label for="edit-node-status"> Approval Status </label>
      <div class="views-widget">
        <div class="form-item" id="edit-node-status-wrapper">
          <select name="node_status" class="form-select" id="edit-node-status"><option value="All" selected="selected">&lt;Any&gt;</option><option value="0">queued</option><option value="1">approved</option><option value="2">denied</option></select>
        </div>
      </div>
    </div>

<!-- Select SHOULD go here -->

  <div class="views-exposed-widget views-submit-button">
    <input type="submit" id="edit-submit-wpg-content-bulk-admin" value="Apply" class="form-submit">
    <div class="form-item" id="edit-test-wrapper">
      <label for="edit-test">New Filter: </label>
      <select name="test" class="form-select" id="edit-test"><option value="0">value 1</option><option value="1">value 2</option></select>
    </div>
  </div>

</div>

Does anyone know what I'm missing here? Why would the newly added 'test' element be rendered inside the submit element wrapper?

1
  • I had the same problem and had to use an #after_build function to workaround.
    – AKS
    Apr 29, 2013 at 16:17

1 Answer 1

5

You need to turn the new form item test into a widget by declaring the item in $form['#info']

So your code should look like the following:

function my_module_form_alter(&$form, &$form_state, $form_id){
  $form['test'] = array(
    '#type' => 'select',
    '#title' => 'New Filter',
    '#options' => array('value 1', 'value 2')
  );

  $form['#info']['test'] = array(
    'value' => 'test', 
  );
}
1
  • Thanks! I was having this problem too but didn't realize how to fix it.
    – AndraeRay
    Jul 25, 2017 at 18:37

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.