1

I have 2 separate views that return a set of results for each of my content types. The fields are different, but the filters are exactly the same - Sort by Location / Distance. This Distance / Proximity filter is exposed, and it is set to 'remember' the last entry. I followed the tutorial here http://drupal.org/node/359463 to be able to filter results by distance. It works great.

In views, to remember the last entry, Views uses sessions. But, each view has its own individual $_SESSION variable. That means what I set for one view does not carry over to the next, and even each view can have different values, which is not what I want. Here is a dump of the session after they have been set:

[views] => Array
    (
        [search_pages] => Array
            (
                [default] => Array
                    (
                    )

            )

        [baby_clothes_pages] => Array
            (
                [default] => Array
                    (
                        [distance] => Array
                            (
                                [postal_code] => 11211
                                [search_distance] => 100
                                [search_units] => mile
                            )

                    )

            )

        [baby_gear_pages] => Array
            (
                [default] => Array
                    (
                        [distance] => Array
                            (
                                [postal_code] => 22343
                                [search_distance] => 100
                                [search_units] => mile
                            )

                    )

            )

    )

What I would like to do is create a small module or snippet of code that will simply allow someone to type in their 5 digit zip code and then it update all the values across all my pages with this filter. If it is changed on any page, then it changes it for the rest of the pages as well. It also needs to work so that it affects the results on the next page load so that it correctly shows them filtered.

Here is a snippet of code I was experimenting with to try to set this value, but it's the wrong direction.

 $all_views = views_get_all_views();            // get an array of all views in use on the site

foreach ($all_views['category_pages']->display as $key => $value) {
 //check to see if the structure exists I am looking to modify
if(array_key_exists('postal_code', $all_views['category_pages']->display[$key]->display_options['filters']['distance']['value'])) {

//if so, set its value
$all_views['category_pages']->display[$key]->display_options['filters']['distance']['value']['postal_code'] = '28152';

//try setting the session instead
$_SESSION['views']['category_pages'][$key]['distance']['postal_code'] = '28152';
    }

What's the correct direction? How do I get this value changed? Could it be done in the template.php file with preprocess function? Should I do it in a module, and if so, what do I need to alter?

Very lost at this point. Thanks for the direction!

  • note I found this page groups.drupal.org/node/10129. Four bullets down under API it suggests that the filter value can be changed. But this code is so vague. Where does this code go? In a module? Also, is it a continuation of the above code, or can I just use that line? – blue928 Aug 10 '12 at 4:39
1

It sounds like you would need to save the value somewhere. Next when a page load event happens (on any page of your site) you need to load the value into the field. I think you might need to create a cookie to do this.

1

The answer was much simpler than I was expecting. I almost had the code working above. By removing the views_get_view and set->display portions, it started working. Here is the finished code.

function zipcode_filter_views_pre_view  ( &$view, &$display_id, &$args) {

    $view->display['default']->handler->options['title'] = "this is the new title";
    $view->exposed_input['distance']['postal_code'] = $_SESSION['zipcode_filter']['zip'];
    $view->exposed_input['distance']['search_distance'] = $_SESSION['zipcode_filter']['distance'];

}

By simply altering the correct variable values in the views_pre_view hook, later parts of the Views execution do the rest of the work. Also, this code fires each time a views page is rendered, so it works for all views, regardless of how many there are. So, by storing the values in a session as Mandeep suggested, it affected all views that had the exposed filter field.

0

Take a look at Views Global Filters module. That will solve your problem.

The project page reads

Use Views Global Filter on sites that feature multiple Views. You want the views to be driven by the same filter selection, for instance a country, a date range, a search term or even proximity to a post code.

-1

$view->exposed_input['distance']['postal_code'] = $_SESSION['zipcode_filter']['zip'];

can explain better which are this variables ['distance'] ['postal_code'] ['zipcode_filter'] ['zip']

I have two views, 2 display, page_1 e page_2 with path_1 e path_2; everyone have own exposed filter (same settings) on page_1 if I use I have this url /path_1?filter_identifier_1=18 (when is selected id term 18)

if I change display view url and select exposed I have this /path_2?filter_identifier_2=18

I have only page variables and name filters and must remember every time that the page change, the previous filter value selected

so code must to be similar at this?

$view->exposed_input['page_1']['filter_identifier_1'] = $_SESSION[previous_page][previous_filter_identifier_value];

$view->exposed_input['page_2']['filter_identifier_2'] = $_SESSION[previous_page][previous_filter_identifier_value];

this if I leave both the exposed filter (first steps for understand how work)

previous_page how can know this variable ? (in the example ther are only 2 display, but how when more?) previous_filter_identifier_value how can know this variable ? is sufficient write only filter identifier name?

other:seen that I must have only one exposed, how can use $view->exposed_input for to set the 2nd filter if isn't exposed and not have the filter_identifier ?

........

  • Try editing your answer properly. – niksmac Dec 7 '12 at 9:02

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.