0

This is for Drupal 7.

I am trying to modify an exposed filter's id in views. Basically we've noticed a use-case with multiple simple hierarchical selects on a given page, in separate exposed filter forms will conflict because they're using the same id attribute in the HTML.

The solution we've come up to to solve this is to append a short hash to the end of this id, making it unique across the page.

The problem is that I'm having trouble figuring out exactly where to do this, and the views objects' immense size doesn't help me in tracking down exactly where I have to make this change.

I'm trying things like this (this is in hook_views_pre_view):

$view->display['DISPLAY_NAME']->handler->options['filters']['field_FIELD_NAME_HASH-HERE'] = $view->display['DISPLAY_NAME']->handler->options['filters']['FIELD_NAME'];

$view->display['DISPLAY_NAME']->handler->options['filters']['field_FIELD_NAME_HASH-HERE']['expose']['identifier'] = 'field_FIELD_NAME_HASH-HERE';

$view->display['DISPLAY_NAME']->handler->options['filters']['field_FIELD_NAME']['expose']['operator_id'] = 'field_FIELD_NAME_HASH-HERE_op';

However this only seems to throw an error.

So how do I change the id on a given exposed filter for a view/display?

EDIT - this is the markup I am trying to affect:

<div class="form-item form-type-select form-item-field-FIELD_NAME shs-wrapper-processed">
 <select class="element-invisible shs-enabled form-select shs-processed" id="edit-field-FIELD_NAME" name="field_FIELD_NAME" style="display: none;"><option value="All" selected="selected"><option></option></select>
<select id="edit-field-FIELD_NAME-select-1" class="shs-select form-select shs-select-level-1 chosen-disable" style="display: inline-block;"><option></option></select></div>

Everywhere that has FIELD_NAME either has the default field name, or if I change it in Views, the changed identifier, however I can't seem to change any of these names programmatically - even with the answer below, which is what I really need to do.

How do I affect this markup?

Here is my current hook_preprocess_views_exposed_form:

if($vars['form']['#id'] == 'views-exposed-form-reference-views-block-1') {
    $number = <NUMBERHERE>;
    $vars['form']['#info']['filter-field_FIELD_NAME']['value'] = 'field_FIELD_NAME_' . $number;
    $vars['form']['field_FIELD_NAME_' . $number] = $vars['form']['field_FIELD_NAME'];
    $vars['form']['field_FIELD_NAME_' . $number]['#id'] = 'edit-FIELD-NAME-' . $number;
    $vars['form']['field_FIELD_NAME_' . $number]['#parents'] = array('field_FIELD_NAME_' . $number);
    $vars['form']['field_FIELD_NAME_' . $number]['#array_parents'] = array('field_FIELD_NAME_' . $number);
    $vars['form']['field_FIELD_NAME_' . $number]['#name'] = 'field_FIELD_NAME_' . $number;
    $vars['widgets']['filter-field_FIELD_NAME']->id = 'edit_field_FIELD-NAME-' . $number;
    unset($vars['form']['field_FIELD_NAME']);
  }

1 Answer 1

0

I think you might be using the wrong tool for the job. It sounds like this is a theming problem, not a views data structure problem (which would use something like hook_views_pre_view().

You can override the template views-exposed-form.tpl.php to override the markup of the exposed form & the preprocess function template_preprocess_views_exposed_form() to override the markup form widgets:

function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) {
  // @TODO: alter markup of $vars['widgets']['filter-MYFILTER']->widget.
}

Alternatively, the views UI provides a means to change the id element by changing the Filter identifier underneath the More fieldset which also changes the URL query parameter used as well.

2
  • I've tried using the hook you suggested, as well as hook_field_widget_form_alter to try to affect the widget, however no luck. How do I change the ID that is displayed on views-exposed-form.tpl.php without overriding the template entirely? Specifically here, among others: <div id="<?php print $widget->id; ?>-wrapper" class="views-exposed-widget views-widget-<?php print $id; ?>">
    – Jack Ryan
    Commented Aug 23, 2016 at 16:02
  • Changing the data in hook_preprocess_views_exposed_form doesn't modify the markup mentioned.
    – Jack Ryan
    Commented Aug 23, 2016 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.