开发者

Remove or Filter Options on Exposed Filter in Drupal 7

开发者 https://www.devze.com 2023-04-13 00:42 出处:网络
I\'m working on a website that uses Domain Access and for a view I expose a filter that lists all domains. I want to list only some domains but not all. I know that it is posible to do with the filter

I'm working on a website that uses Domain Access and for a view I expose a filter that lists all domains. I want to list only some domains but not all. I know that it is posible to do with the filter configuration, but if I limit the options this way there is a problem with ajax which changes all the开发者_运维技巧 options text to "1", and in any case I want to know how to do it, with either hook form alter or with themes, but preferably using some hook from a module.

In general I'm trying to figure out how to remove some of the options for an exposed filter, I managed to do it using JQuery, but I'd like to do it with php, thanks!


It should be fairly straight forward using hook_form_FORM_ID_alter as you suggest. First inspect the <form> element of the filter form (using Firebug or similar) and get it's ID attribute. The form's ID in Drupal will be that string but with all '-' characters replaced with '_' (so 'form-exposed-filter-form' would become 'form_exposed_filter_form').

The thing to remember is that the submit handler for the form will probably be expecting the elements you're trying to remove to be there, so you'll probably get unexpected results if you just yank them out of there.

The way round it is to change the type of the elements to value instead (so they're available to the submit function in $form_state['values']), and choose a default value for each. Something like this:

// Replace 'FILTER_FORM_ID' with the form's ID
function mymodule_form_FILTER_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $form['some_existing_element'] = array(
    '#type' => 'value',
    '#value' => $default_value_for_filter
  );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号