Is there a way to create programmatically exposed filters in Drupal 7?
I searched a lot but I found only topics related remove or alter exposed filters already available in a view.
By instance my view doesn't provide any exposed filter. I must create it through the code in my module. I have a view but I can't change it via Views administration interface. I must achieve the result by implementing hook_views_pre_view() and add_item().
Unfortunately add_item is not very well documented or, at least, not the part concerning the $options array structure.
Given 'field_status' a "select list" with 3 items defined in the field configuration, and the following piece of code:
function hook_views_pre_view(&$view, &$display_id, &$args) {
$field = field_info_field('field_status');
if (!empty($field)) {
$filter = array(
'identifier' => 'field_status',
'exposed' => TRUE,
'is_grouped' => FALSE,
'table' => 'field_data_field_status',
'type' => 'list_text',
'expose' => array(
'operator_id' => 'field_status_value',
'label' => 'Status',
)
);
$view->add_item($view->current_display, 'filter', 'field_data_field_status', 'field_status_value', $filter);
$view->display_handler->set_option('css_class', 'view-status');
}
what should be the structure of the array $options (in my case $filter)?
As a result of this I can see the submit button 'Apply' and the label "Status" in an empty widget 'div'.
Here is the code edited following suggestions in comments:
function my_module_views_default_views_alter(&$views) {
if (array_key_exists('my_view_name', $views)) {
$options = array (
0 => 'Blocked',
1 => 'Active',
);
$display = $views['my_view_name']->display['my_view_page'];
$display->display_options['filters']['field_status']['id'] = 'field_status';
$display->display_options['filters']['field_status']['table'] = 'field_data_field_status';
$display->display_options['filters']['field_status']['field'] = 'field_status';
$display->display_options['filters']['field_status']['exposed'] = TRUE;
$display->display_options['filters']['field_status']['expose']['operator_id'] = 'field_status_value';
$display->display_options['filters']['field_status']['expose']['label'] = 'Status';
$display->display_options['filters']['field_status']['expose']['operator'] = 'field_status_value';
$display->display_options['filters']['field_status']['expose']['identifier'] = 'field_status';
$display->display_options['filters']['field_status']['value'] = $options;
$views['my_view_name']->display['my_view_page'] = $display;
}
}
--- ALTERNATIVE SOLUTION ---
Since this problem seems to be hard to fix (@ least for my specific case) I have used a workaround to achieve the goal. The solution consists basically on a Views Block Clone with Exposed Filters which overrides the Views Block (without exposed filters) according to specific cases (Is User LoggedIn, Content Type, Contextual Filters ecc..).