5

Summary

I have two seperate views on the same page - one Page and one Block that show two seperate content types respectively, but have a filter exposed for a field that they share.

On the page of the first view, I have attached a block display of the second into a new region. I want the filter values of the exposed filter of the first view to also feed into the filter values of the second block view, if possible.

How it's currently set up

I have two views.

The first view, "Local branches", is a Page display that surfaces content of type Local branch. This content type has a Location field, powered by the Location module. On the view page, there is an exposed filter criteria:

Location: Distance / Proximity

which is exposed to the user as a text field and the origin in the filter settings set to Postal Code.

The code is then sorted by Distance / Proximity.

On the view page therefore, there is a text field where users can enter in a post code and it will return results closest to that Postal code.


The second view, "Shops", is a Block display that surfaces content of type "Shops". This content type is almost identical to the "Local branch" content type, in that it also has a Location field.

In this view, I have again an exposed filter:

Location: Distance / Proximity

that is set up the exact same way - but note that this is a Block display. It's again sorted by Distance / Proximity.


Finally, on the Page of the first view, "Local branches", I have placed the second view's Block, "Shops" in a seperate region.

My hope is that the Shops block will somehow pick up the exposed filter settings from the URL built by the "Local branches" view, which looks like this:

http://www.example.com/local-support?distance[postal_code]=EN11+8RX&distance[search_distance]=100&distance[search_units]=mile

As you can see there are three parameters that power the exposed Postal code search form. Here's a crude diagram:

Rough diagram of the view page

Problem

The Block view, "Shops", doesn't respond to the filter values in the URL, but just outputs all results sorted arbitrarily (there is no other sort atm). I would like it to respond with results as if it were using the exposed filter values from the "Local branches" view as its own exposed filter.

Potential solutions and queries

One thing I did try was to programatically create the "Shops" block, and render it on the view page /local-support and see if I could use the $_GET parameters and feed it directly in to the view. Code to generate the output looks something like this:

function _mymodule_shop_block_view() {
  $view = views_get_view('shops');
  $view->set_display("block_1");
  $view->set_arguments($args);
  $view->pre_execute();
  $view->execute();
  $content = $view->render();

  return $content;
}

The problem is I'm not sure what the value of $args would actually be in this case. I want to exectue and render the view, but pass the $_GET params in somehow, but they're exposed filter params so I don't know if $view->set_arguments is actually appropriate in this case?


My other thought that was because the "Shops" view is a Block display, the exposed filter won't work as it should. Could I try building it as a page display, expose it the Location filter as a block, and then rendering it in a block? I could hide the exposed filter in CSS (bad I know but if you haven't guessed by now I kinda wanna get this working).


Could attaching the block to the Page view integrate it - is it possible to do view attachements across separate views?


Thanks for taking the time to read this - I hope there's enough info about what I am trying to achieve!

1
  • 1
    Give this a try, I know it works when you have the same exact view, but idk if having diff content types will affect it. In footer click add, and select view attachment. Now try filtering and see what happens.
    – No Sssweat
    Oct 27, 2016 at 8:14

2 Answers 2

3
+400

Yes, it's possible to have 2 different views on the same page sharing the same exposed filter query parameters.

The trouble you're having is, by default, a block display doesn't show any exposed filters. The only way to enable this is by enabling the Use AJAX setting on the block view under the Advanced section in the admin UI. Once set, both the block & page Views will pick up on the same location[postal_code]/location[distance]/location[search_units] query parameters.

Of course this means, you'll have 2 different form filters on the same page but you can kludge your way around it with a style rule to hide it (e.g. #block-views-my_block_machine_name-block div.view-filters { display: none; }) or some other means of hiding the form.

1
  • Thanks - I did it a slightly different way in the end by creating a new attachment display in the first view (set to a different content type) which also did the job. But I will use your method in the future as it allows it to go into a seperate block which is more manageable/themeable.
    – njp
    Nov 1, 2016 at 14:15
0

This module works great for exactly this issue:

https://www.drupal.org/project/views_contextual_filter_query

The exposed filter of one view (probably the page view) turns into a query parameter. Just use the key of the query parameter as contextual filter value under "provide default argument" >> "parameter name". Done and done.

Your Answer

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

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