1

I would like to remove a certain exposed filter on the taxonomy term view when certain conditions in the URL are met.

For example, I want to remove the "Color" exposed filter when the user is seeing the /color/red taxonomy term page, leaving only the "Shape" filter available.

Or remove the "Shape" exposed filter on the /shape/square page, leaving only the "Color" one.

The only code I could come up with is this, which is ugly and doesn't even work.

function hook_views_pre_render(&$view){
      if( CONDITIONS_ARE_MET ) {
         unset($view->filter['field_color_tid']);
         unset($view->exposed_data['field_color_tid']);
         unset($view->exposed_raw_input['field_color_tid']);
      }
}

Any ideas?


UPDATE

This is the code in mmo_views_alters:

function mmo_views_alters_views_pre_build(&$view){
    //dpm($view);
    unset($view->filter['field_tipologia_tid']);
}

This is the exported view: http://pastebin.com/JrYDmtZQ

It has 4 filters, and 3 of them are exposed. I'm using BEF for displaying them, which generated another warning (undefined index filter-field_tipologia_tid) that I solved by patching better_exposed_filters_exposed_form_plugin.inc, line 896:

if( isset($form['#info']["filter-$label"]['value']) )
        $field_id = $form['#info']["filter-$label"]['value'];
else continue;

The warnings that I'm not able to solve are:

Notice: Undefined index: field_tipologia_tid in views_handler_filter_term_node_tid->exposed_validate() (linea 258 di /var/www/drupal/sites/all/modules/views/modules/taxonomy/views_handler_filter_term_node_tid.inc).

Notice: Undefined index: field_tipologia_tid in views_handler_filter_term_node_tid->exposed_validate() (linea 259 di /var/www/drupal/sites/all/modules/views/modules/taxonomy/views_handler_filter_term_node_tid.inc).

They appear even with BEF disabled.

2

Implement hook_views_pre_build() and unset using unset($view->filter['field_color_tid']);

Else, you can also try altering the exposed form in hook_form_alter() using the code $form['field_tipologia_tid']['#access']=FALSE;

  • thanks, that works, although I get 2 similar warnings: Notice: Undefined index: field_tipologia_tid in views_handler_filter_term_node_tid->exposed_validate() Notice: Undefined index: field_tipologia_tid in views_handler_filter_term_node_tid->exposed_validate() (line 259 in /var/www/drupal/sites/all/modules/views/modules/taxonomy/views_handler_filter_term_node_tid.inc) – Sifro Jun 28 '14 at 10:01
  • can you share the code written in hook_views_pre_build() and how many filters you have and how many you are exposing . – wolverine Jun 28 '14 at 10:34
  • sure, I've edited the original post. – Sifro Jun 28 '14 at 11:48
  • @Sifro I have updated the answer, try it – wolverine Jun 28 '14 at 14:03

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.