0

So it seems that Matrix fields don't work with the object syntax so I've changed my field structure to use standard dropdowns, checkboxes and a number field.

I have the dropdown (gender) and number field (maxPrice) working fine, but I can't seem to figure out what to enter for the checkboxes. I assumed it'd be in a similar format to what I've seen elsewhere, as you can see below:

<h1>Search Results</h1>

{% set tags = craft.request.getParam('tags') %}

{# Gender is a dropdown, so it's a single value or no value
 # - If an empty value is selected, it returns both genders #}
{% set gender = craft.request.getParam('gender') %}

{% set maxPrice = craft.request.getParam('maxPrice') %}
{% set maxPrice = maxPrice is not empty ? maxPrice : '99' %}

{% set sessions = craft.request.getParam('sessions') %}
{% set sessions = sessions is not empty ? sessions : ['skype', 'chat', 'phone', 'email'] %}
{% set sessions = ['and']|merge(sessions) %}

{% set hours = craft.request.getParam('hours') %}
{% set hours = hours is not empty ? hours : ['office', 'overtime', 'weekends'] %}
{% set hours = ['and']|merge(hours) %}

{% set results = craft.users({
  group: 'verified',
  search: "tagsSpecialismSearchable:#{tags}",
  gender: gender,
  profilePricingSession: [
    'and',
    '> 0',
    "<= #{maxPrice}"
  ],
  profileSessions: sessions,
  profileHours: hours,
}) %}

{% set users = shuffle(results) %}

Everything works except profileSessions and profileHours, which are both checkboxes. I assumed it might be that they need to be passed a key: value pair, i.e: skype: true or some other method, but nothing I've tried so far is working.

I happened on a post saying checkboxes store in a query unfriendly way, so perhaps I'm better off changing the field again for something more friendly. They'd need to be multi-select of course, so categories would be the best bet if what I'm doing isn't possible.

Suggestions?

Rob
  • 865
  • 6
  • 13
  • @carlcs Not quite, but if the sentiments are the same: i.e. checkboxes aren't searchable in this way, I guess I'll have to go with categories. If possible, could you point me in the direction of a post that uses categories with the object syntax in a similar way? Would I use relatedTo? (it still confuses me a bit, especially when using field, sourceElement and targetElement) – Rob Jan 16 '16 at 14:44
  • How many checkboxes / categories do you have, @Rob? Have you considered using lightswitches? – carlcs Jan 16 '16 at 14:57
  • Here's an example for a dynamic relatedTo parameter: Dynamic relatedTo queries (categories are most likely to be the targetElement, as they are the elements selected in the field and not the element the field is attachted to). – carlcs Jan 16 '16 at 15:02
  • @carlcs Yeah that's the thing, I only have about 8 in total which seems overkill to use categories, but lightswitches might look a bit fuggly on the backend (the users can edit their profile in the CP). Is that a similar method to dropdown: 'value' or would it be something like field: true or field: '0'? Craft is brilliant but these little intricacies can take some getting used to — hopefully the v3 api will give us more options :) – Rob Jan 16 '16 at 15:08
  • I have written a checkbox fieldtype (note the singular) for a project, which stores the data like a lightswitch field, but looks like the checkboxes field in the CP. I'll push it to GitHub for you, so you can keep the looks in the CP, but store in a query friendly way. ;-) – carlcs Jan 16 '16 at 15:12
  • @carlcs Sounds great! Be good to see how you manage it .. Is it future compatable? So many ways to build a thing on Craft :) – Rob Jan 16 '16 at 17:08
  • 1
    Just released it: https://github.com/carlcs/craft-checkboxfield – carlcs Jan 16 '16 at 17:33
  • @carlcs Perfect, looks like it'll come in handy ... thanks! – Rob Jan 18 '16 at 15:58
  • @carlcs I'm struggling to get your Dynamic relatedTo query example working, if using for multi-selectable categories in 2 category groups. I think this example is closer to what I'm looking for. Am I right in thinking the only way to do this is adding a for loop through all the query[]= group arrays? I don't think I fully understand what your example is doing. – Rob Jan 19 '16 at 21:46
  • FYI I'm using slugs as values for the checkboxes. I assumed at first what you were doing is passing query.art parameter array into .id() and merging each with it's own { targetElement: categoryName }, but I thought .id() and .slug could only take one string at a time. Is it something similar to the second example in Twig docs which (due to being wrapped in [] creates a list of key: value pairs? Or am I off the mark? I come from a front-end background so this stuff makes my head hurt! – Rob Jan 19 '16 at 22:01
  • @badlydrawnrob on Slack ... I almost have it figured out thanks to Twig dump but the search results are doing some funky things. – Rob Jan 19 '16 at 23:30

0 Answers0