Briefly:

I have a view with:

  • Two text-list fields, one from content type A, and one from content type B.
  • An exposed filter to select only one of those content types to query/display.
  • Two exposed filters with select list widgets, one for the field from content type A, and one for B.
  • All filters are optional.

If the exposed filter for the field from "A" has any value selected, and content type B is selected, then there are no results, even if there are values selected for the field from B. Uncheck the values for the field from A, and the view works.

I would have expected that at least the query would have been run for nodes from B.

Details:

I have two content types, "condo", and "house". In "condo" I have a field "condo_bedrooms", and in "house" I have the field "house_bedrooms". I display both fields in the view and filter both fields using check boxes from BEF. The fields are integer. I also filter for content type.

If I select only the house content type, and select "1" for house_bedrooms, the view works. But if I leave the checkbox for "1" in house_bedrooms checked, with no selection for condo_bedrooms and then change the content type to "condo", the view produces nothing. Ditto for the converse. If both bedroom filters are checked (any value) it also fails for either or both content types selected.

The view always fails when both bedroom filters have selected values, regardless of which (or both) content types are selected.

I tried "ORing" the two bedroom filters but got the same results.

I tried using the "Views Dependent Filters" module but it only hides the filter - it doesn't disable it or even reset the selection when hidden, despite claims that hidden filters with values are ignored.

I tried this is D6 and D7. Tried basic filter mode (no BEF) too.

The filter order has no effect.

When the view works (only one bedroom filter selected) it produces this SQL:

SELECT node.nid AS nid, node.created AS node_created, 'node' AS field_data_field_house_bedrooms_node_entity_type, 'node' AS field_data_field_condo_bedrooms_node_entity_type
FROM
{node} node
INNER JOIN {field_data_field_condo_bedrooms} field_data_field_condo_bedrooms ON node.nid = field_data_field_condo_bedrooms.entity_id AND (field_data_field_condo_bedrooms.entity_type = 'node' AND field_data_field_condo_bedrooms.deleted = '0')
WHERE (( (node.type IN  ('condo_unit')) AND (field_data_field_condo_bedrooms.field_condo_bedrooms_value = '1') ))

When it fails (both bedroom filters selected):

SELECT node.nid AS nid, node.created AS node_created, 'node' AS field_data_field_house_bedrooms_node_entity_type, 'node' AS field_data_field_condo_bedrooms_node_entity_type
FROM
{node} node
INNER JOIN {field_data_field_condo_bedrooms} field_data_field_condo_bedrooms ON node.nid = field_data_field_condo_bedrooms.entity_id AND (field_data_field_condo_bedrooms.entity_type = 'node' AND field_data_field_condo_bedrooms.deleted = '0')
WHERE (( (node.type IN  ('standalone_house')) AND (field_data_field_condo_bedrooms.field_condo_bedrooms_value = '1') ))

So basically, I cannot have any values in a filter selection on a field in a deselected content type. I would have expected the query to ignore the filter for the deselected content type, or at least not show no results for the selected content type. As you can see, Views won't even run the query for the bedroom field in the selected content type, when the bedroom filter in the other content type has a value selected. Weird.

The quickie solution is to make the bedroom fields shared, but that's a little restrictive in this case for various reasons.

I had solved this problem some years ago with D6 - Views 2, by writing a jQuery script to hide one of the bedroom filters and clear its values if the opposite node type was selected. Works well but I was hoping to find a cleaner solution in Views 3.

I presume this is a limitation in the way Views is designed, but hoping there's an easy way to fix it without having to resort to the jQuery script.

I originally posted this in the Views issue queue as a support request but got no response. You can get the view file there: #1805134: Filtering on two fields in different content types and filtering on content type fails

Comments

nevets’s picture

Views will not handle that case as far as I know.

I would suggest using a single field called bedrooms in both content types. Add it to 'house', then in 'condo' use 'Add an existing field'

druplicate’s picture

That's what I was afraid of. Seems it wouldn't be that difficult to add some simple logic to fix it. I thought that was the intention of Views Dependent Filters, but it doesn't appear to work that way.

Yes, as I mentioned in the post, I can always share the fields, but for various reasons, that's a bit restrictive for this use case - I want different settings for the two fields which I can't do if they're shared, for example.

My simple jQuery script fit the bill nicely, so unless anyone has a better idea, I'll just do that. I posted over at Views Dependent Filters that it doesn't reset the fields when hiding the filter, but it seems the lights are out over there.

I would think this issue would be rather common.

druplicate’s picture

The readme at Views Dependent Filters gives a hint as to why this fails in Views: http://drupal.org/node/1805134#comment-6604596