Hi. I'm trying to change two exposed form filters inside a view. The filters were both configured to be textboxes in the view, but I'm trying to change them into select boxes. Here is the code:
include_once '..client_names.inc';
function rwk_activity_filter_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] == 'views-exposed-form--last-activity-page-4') {
//array of options for the first filter
$options = array(
'Resolved' => 'Resolved',
'New' => 'New',
'Open' => 'Open',
'Stalled' => 'Stalled',
);
$form['TicketStatus']['#type'] = 'select';
$form['TicketStatus']['#options'] = $options;
$form['TicketStatus']['#default_value'] = array();
$form['TicketStatus']['#multiple'] = True;
$form['TicketStatus']['#size'] = 4;
//the following code is for the second filter
//querying a custom table in the db to populate an array with the names of the "clients"
$sql_query = 'SELECT Name FROM _clients';
$result_clients = db_query($sql_query);
$result_clients = $result_clients->fetchCol();
$clients_options = array();
foreach($result_clients as $client) {
$clients_options[$client] = (string)$client;
}
$form['Name'] = array(
'#type' => 'select',
'#options' => $clients_options,
'#default_value' => '',
'#multiple' => True,
'#size' => 10,
);
}
}
Now, this code works in that it changes both of the filters into select boxes. However, I'm having two problems. The second filter ($form['Name']), whenever I select one of the choices in the select box and press the apply button, the results are not filtered and I get the following warning:
Warning: addcslashes() expects parameter 1 to be string, array given in DatabaseConnection->escapeLike() (line 965 of /includes/database/database.inc).
Also, in the first filter($form['TicketStatus']), if I select only one of the options (such as 'New') within the select box and press the apply button, then the filter works! However, if I select multiple values and then press the apply button the filter returns no results.
Any help with one or both of these problems would be greatly appreciated. Thanks!
Comments
Comment #1
yevgeny.ananin commentedOk, for the first error (addcslashes expects ...), I figured out that the problem was that I had the original filter for 'Name' set to 'Contains' as the operator instead of 'Is Equal To'.
I still can't do multiple select on the 'Status' filter. However, there is a setting in admin/structure/views/settings that lets you see the actual sql query that your view is using. I can see that when I try to select two values within the status drop down, the query has this in the WHERE clause: WHERE (( (_last_activity.TicketStatus LIKE 'New', 'Open' ESCAPE '\\') ))
So, the query is using the wrong syntax, it should be: _last_activity.TicketStatus LIKE 'New' OR _last_activity.TicketStatus LIKE 'Open' ...
I have no idea how to change the query though.
Comment #2
yevgeny.ananin commentedOk. So the second problem I was having (I was able to change the filter to a select box, but I wasn't able to select multiple values because the resulting sql query used the wrong syntax).
I was able to solve this problem by using the hook_views_query_alter(&$view, &$query) hook. Here is my code:
Essentially what I did was, I modified the view's query by searching for the two filters (TicketStatus and Name) and setting the operator to 'IN'; where the default value of operator was 'LIKE'.
Comment #3
mustanggb commented