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!

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

2 Answers 2

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.

4
  • Side note, if you need the OR version use ',' instead of '+'
    – sareed
    Commented Aug 27, 2015 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. Commented Aug 28, 2015 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
    Commented Aug 28, 2015 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
    Commented Aug 28, 2015 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.
1
  • #1 works well! It solved my issue. Commented Nov 7, 2017 at 6:12

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.