3

I have a contextual filter set up to allow multiple values, and it filters the view on the city. so if I pass in "new york", the view will look for "new,york" which will not return any results. If I pass in "new york,chattanooga" the view looks for "new york,chattanooga" which will give me the results from new york and chatanooga. Lastly, if I pass in "new york, "(notice the space), the view will give me the correct results.

Is there a way to get the view to not act this way? I.E. Allow me to do views_embed_view('places_to_stay', 'block', "new york");? Thanks!

  • What type of field is the "city" field? Different views handlers are used depending on the type of field. – Aaron Aug 27 '15 at 21:30
  • The field is part of an address field, so its the locality. – user2990286 Aug 28 '15 at 14:20
1

in your case I think you need to separate values with plus-characters. so it would be:

views_embed_view('places_to_stay', 'block', "new york+chattanoog");

Because you have a single argument expecting multiple inputs.

in some other cases you might use the comma separator if you have multiple arguments each expecting a single inputs.

  • Side note, if you need the OR version use ',' instead of '+' – sareed Aug 27 '15 at 21:34
  • Doesn't the + do and? so it would look for items that have New York AND Chattanooga in the value? I want or so it shows items in New York OR Chattanooga. – user2990286 Aug 28 '15 at 14:22
  • Yes. you can separate them with commas as Sareed mentioned in his comments. so it would be : views_embed_view('places_to_stay', 'block', "new york", "chattanoog"); – sam2de Aug 28 '15 at 14:44
  • This doesn't answer the OP's question. They expect to be able to call views_embed_view('places_to_stay', 'block', "new york"); and have it work. – Aaron Aug 28 '15 at 17:00
1

This is, unfortunately, the way that the views_break_phrase_string() function is designed to work. Taken from the comments in that function:

  // Determine if the string has 'or' operators (plus signs) or 'and' operators
  // (commas) and split the string accordingly. If we have an 'and' operator,
  // spaces are treated as part of the word being split, but otherwise they are
  // treated the same as a plus sign.

So you seem to have 2 alternatives:

  1. Use the "new york, " syntax that you don't prefer to force it to treat your argument as an "and" argument and thus not split on the space.
  2. Create your own custom views handler that calls your own. customized version of views_break_phrase_string().

To do option 2 you would need to do a few things:

  1. Copy views_break_phrase_string(), rename it, put it in your own module, and change its logic to not split on spaces.
  2. Create a custom views argument handler that extends views_handler_argument_string but only implement the query() method. It can be an exact copy of the query method from views_handler_argument_string but with a call to your custom version of views_break_phrase_string() instead. Do the same for the title() method, too, if you plan to use it since it also calls views_break_phrase_string().
  3. In your custom module, implement hook_field_views_data_alter() and change the argument handler for your addressfield locale field to use your new custom argument handler.

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.