adrian Posted April 5, 2013 Share Posted April 5, 2013 Hi guys, I want a simple selector to test if a checkbox field is empty. hide_from_top_menu=1 works as expected and finds those that are checked, but hide_from_top_menu!=1 does not work - it returns nothing. Now I understand that there is no entry in the hide_from_top_menu field for a page until the checkbox is checked and I guess that is the problem. I am using this with MarkupSimpleNavigation. Obviously I can easily hack the code of the module to add in a check something like this: if($child->hide_from_top_menu != '1'){ but I feel like I should be able to achieve this more cleanly. Obviously I could also turn this field around to be "show_in_top_menu", but since there is no way to set default values for fields, I'd rather avoid having users have to check this for each new added page. Maybe not a big deal, but thought this might be an issue at a more general level Thanks! Link to comment Share on other sites More sharing options...
adrian Posted April 5, 2013 Author Share Posted April 5, 2013 Just a quick followup - I have decided to take a different approach to the problem, so I don't really need any specific help, but I'd still be curious to know if it's possible to use a selector to find all pages with unchecked instances of a checkbox. Link to comment Share on other sites More sharing options...
kongondo Posted April 5, 2013 Share Posted April 5, 2013 Does something similar to below not work? if($page->hide_from_top_menu == 0){ } See this thread and the solution (post #9) as well as the issues about '=' versus '=='. Can't find it now but if I remember correctly, in PW checkbox is either 0 (unchecked) or 1 (checked). I could be wrong /k 2 Link to comment Share on other sites More sharing options...
adrian Posted April 5, 2013 Author Share Posted April 5, 2013 kongondo - thanks! I couldn't use the approach you suggested, because I needed to use a PW selector within the nav module. But, even though I was sure I had tried it, I just tested a '=0' selector and it does indeed return all pages without that field checked. It returns all pages, whether they have the field in their template or not, you need to add an additional selector to limit results to a parent page, or perhaps a template. But it does work - not sure how I missed it before - tired brain I guess! As for '=' vs '==' - if you're using a PW selector it needs to be '='. If you are doing something in regular php it needs to be '==' or '===' depending on what you are comparing Link to comment Share on other sites More sharing options...
Luis Posted April 5, 2013 Share Posted April 5, 2013 You could check checkboxes like this: if($checkbox) and if(!$checkbox) example: if($page->checkbox) echo 'i am set'; if(!$page->checkbox) echo 'i am not set'; So to modifiy your output in a foreach just put your markup in an if condition. 1 Link to comment Share on other sites More sharing options...
adrian Posted April 5, 2013 Author Share Posted April 5, 2013 Thanks Luis - also good options, but I was trying to doing it just through a PW selector so I could use the selector option that Soma built into the SimpleNav module. Turns out the '=0' in a selector works great, but I still actually ended up going with a different approach just because it made more sense in the current situation Link to comment Share on other sites More sharing options...
Luis Posted April 6, 2013 Share Posted April 6, 2013 Would you share your solution with us? Link to comment Share on other sites More sharing options...
adrian Posted April 6, 2013 Author Share Posted April 6, 2013 In the end I think it suited the purpose better to check against templates like this: "selector"=>"template!=person" because I don't want the children of certain pages to show in the menu - things like a staff directory for instance where each child page is a person and has the template 'person'. Link to comment Share on other sites More sharing options...
Manfred62 Posted April 6, 2013 Share Posted April 6, 2013 because I don't want the children of certain pages to show in the menu - things like a staff directory for instance where each child page is a person and has the template 'person'. depends on the structure of the site, but couldn't this also be done with a setting in MarkupSimpleNavigation? 'max_levels' => 1, // set the max level rendered 1 Link to comment Share on other sites More sharing options...
adrian Posted April 6, 2013 Author Share Posted April 6, 2013 That's actually a really great idea - I hadn't really noticed that option before. I think in my situation it will work perfectly. So many options, so little time Link to comment Share on other sites More sharing options...
Manfred62 Posted April 6, 2013 Share Posted April 6, 2013 soma has a detailed documentation on github for MarkupSimpleNavigation Link to comment Share on other sites More sharing options...
adrian Posted April 6, 2013 Author Share Posted April 6, 2013 Yeah - it is a great module and great docs - for some reason, my brain just didn't think of that approach in this situation. I am actually using a combined template!= and max_levels set of options to achieve what I need. Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 27, 2014 Share Posted February 27, 2014 I had this same question. For some reason I can't determine, checkbox=0 would work in some instances but not others. The solution I found that worked in all instances was !checkbox=1. This returns all pages where the checkbox is either unchecked or it doesn't exist at all. Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 They all work. I mostly use check= Never experienced what you say. Link to comment Share on other sites More sharing options...
dragan Posted February 27, 2014 Share Posted February 27, 2014 hide_from_top_menu<1 or hide_from_top_menu<1 should work too, afair Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 !field=value doesn't exist. (Edit: for checkboxes) Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 27, 2014 Share Posted February 27, 2014 !field=value doesn't exist. What do you mean? It is in the docs. checkbox!=1 returned nothing and checkbox=0 worked in some cases but not others. As soon as I replaced it with !checkbox=1 (nothing else was changed in the selector or elsewhere) it worked in all cases. checkbox= seems to behave the same as checkbox=0. This is with PW 2.4. I am using this in the selector for my global site search to not return results from pages which have the checkbox checked. This is what the selector looks like: $matches = $pages->find("body|sidebar|field|another_field%=$q, limit=50, !checkbox=1"); Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 I meant for checkboxes !field doesn't make sense, does it? Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 27, 2014 Share Posted February 27, 2014 I meant for checkboxes !field doesn't make sense, does it? I don't see why not. But I can tell you that it works.... Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 It is the same as "checkbox=0" or "checkbox=" (these all return the expected results for me) Can you tell where or how you know it's not returning expected result? I mean I use checkboxes all the time and never noticed something wrong with it. Maybe a mysql thing? What PW version? Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 hide_from_top_menu<1 or hide_from_top_menu<1 should work too, afair it does not for checkboxes, actually. Link to comment Share on other sites More sharing options...
thetuningspoon Posted February 27, 2014 Share Posted February 27, 2014 It is the same as "checkbox=0" or "checkbox=" (these all return the expected results for me) Can you tell where or how you know it's not returning expected result? I mean I use checkboxes all the time and never noticed something wrong with it. Maybe a mysql thing? What PW version? See my last post for the selector I'm using. It is for the site search. PW 2.4. So it is using Ryan's default search.php template with some modifications. I am also doing a similar selector for other pages in which I am just getting the children of the page, and there checkbox=0 works fine. The only thing I can think of is that I'm using the %= on the earlier part of the selector for the more thorough search functionality, but I don't know how or why that might effect the checkbox=0 part... Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 How could we reproduce? Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 I was able to reproduce when using a %= on multiple fields before the checkbox=, it would return results found with the %= search and checkbox checked. As soon as I move the checkbox=0 before the title|body|summary%=value it works again. Link to comment Share on other sites More sharing options...
Soma Posted February 27, 2014 Share Posted February 27, 2014 Issue https://github.com/ryancramerdesign/ProcessWire/issues/378 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now