4

I have a view that I want to change two items right before the page loads - the title, and the zipcode that determins the location to filter by.

My module detects a person's location and replaces the default value in the Views filter so that the results are properly sorted by that area.

So, the title changes to 'Viewing items around Charlotte, NC' where Charlotte, NC is the dynamic element.

I have created a module, but I am not sure what views hooks I need to use to achieve the results, especially getting the view to execute with the new location.

Here are the views hooks for D7 http://api.drupal.org/api/views/views.api.php/group/views_hooks/7

Do I pre_view it? pre_render? Alter the query with the new location details before its executed?

Here is a sample of my module trying to use hook_views_pre_view

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

    $view = views_get_view('baby_clothes_pages');
    $view->set_display('default'); 
    $view->set_exposed_input(array('postal_code' => $zipcode));
    $view->display['default']->handler->options['title'] = $title;

}

note: this reference influeced the code I used. However, the documentation is wanting. Am I even on the right track with this? http://groups.drupal.org/node/10129

4
  • Looks fine. Obviously you'd want to test that you only run this code when looking at a specific view so you don't end up with this function firing over and over, infinitely Commented Aug 11, 2012 at 2:33
  • well, if I do a dpm($view) then it correctly shows those variables populated with the fields that I wanted to populate, but the page renders with the default values. So there is my problem. I have tried all the hooks I know to try, but I cannot get the page to render with my new values. Thoughts?
    – blue928
    Commented Aug 11, 2012 at 7:34
  • This is super basic but I've had to be told it: Make sure you clear the cache whenever you add a new hook.
    – beth
    Commented Aug 11, 2012 at 8:17
  • Gosh, I was really hoping that would have been it, but alas, it did not help. If I put these two lines in my code: $view->display['page_1']->display_options['title'] = "this is the changed title"; dpm($view->display['page_1']->display_options['title']); then, after I clear cache, it correctly shows that the title in the object has been changed (via dpm($view). However, the page still renders with the default title I set via the Views UI for that view. note that says page_1 but I have tried for 'default' as well
    – blue928
    Commented Aug 11, 2012 at 8:29

1 Answer 1

3

The answer was to remove the views_get_view function as well as the set->display function. There was also no need to try to execute the exposed input. All that was needed is to simply set the default values for the variables I wanted in the views_pre_view hook, and then Views takes care of the rest as it finishes doing its thing. Here is the resulting, correctly working 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'];

}

Although, I don't understand why this works - Why do I not need to specify a display? Why did having the other parts of earlier code cause it to not work properly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.