Pedro Posada | Views argument dynamic filters

Views argument dynamic filters

Posted on October 19, 2008 - 1:29pm
<< 30 of 54 >>
Submitted by pedro on November 7, 2008 - 3:46pm.

As a small note to dynamic filters, for example:

<?php
$my_view
= views_get_view('my_name');
$my_view->filter[1]['value'] = 1;
$my_view->is_cacheable = 0;
?>

Always use <?php $my_view->is_cacheable = 0;?> after adding filters, fields, arguments or anything dynamically to a view.

Submitted by pedro on March 7, 2011 - 1:39am.

Handy trick to turn views exposed filters into dropdowns/selects fields.

I tried to override the views-exposed-form.tpl.php by using function MYTHEME_preprocess_views_exposed_form(&$vars, $hook) but that never actually worked. The changes were not being printed out, the form was never altered.

Then I implemented "hook_form_FORM_ID_alter" to convert a textfield exposed filter into a dropdown or select field, I created a custom module and implemented the hook_form_FORM_ID_alter as follows:

<?php
function mymodule_form_views_exposed_form_alter(&$form, &$form_state)
{
    if(
$form['#id'] == 'views-exposed-form-myviews-page-1')
    {       
        unset(
$form['title']);

            
// this part is very important to avoid "illegal choice"
             // error message.
       
$options[""] = t("Any");
       
            
$result = db_query("SELECT title FROM {node}");
        while(
$r = db_fetch_array($result))
        {
           
$options[check_plain($r['title'])] = $r['title'];
        }
       
$form['title']['#type'] = 'select';
       
$form['title']['#options'] = $options;
       
$form['title']['#multiple'] = false;
    }
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copy the characters (respecting upper/lower case) from the image.