I am looking for a way to present the following to users. This will be a list of checkboxes:

[ ] Region 1 A
[ ] Region 1 B
[ ] Region 1 C
[ ] Region 1 D
[ ] Region 1 E
[ ] All of Region 1
[ ] Area 2
[ ] Area 3
[ ] Area 4

This would all be stored in the same field.

So there are two main things I want to be able to do:

1) When entering the data, the admin/user should be able to choose any of the options, but if the select "All of Region 1", it automatically selects all of the Region 1 choices along with the "All of Region 1" choice.

2) When site visitors are searching this field (through an exposed Views filter), if they choose to filter for "All of Region 1" they should get anyone who chose any one of the Region 1 areas as well as anyone who chose "All of Region 1".

One solution would be to just remove the "All of Region 1" option from the exposed filter.

Or have the "All of Region 1" option in the exposed filter also check all the region 1 options automatically.

Is this necessarily a hook_form_alter solution?

  • I would use javascript to do that, something like this answer. – Darvanen Feb 16 '15 at 3:31
  • As I said in reply to f0ns below, I get the javascript but am not yet clear how to add this javascript to the forms in Drupal. – Christopher Dunning Feb 16 '15 at 13:15

This is a possible solution using javascript that shows how to select the child checkboxes with JQuery.

  • Yes, this is a nice solution: to make the checkboxes a set of <ul> and <li> sets and have JQuery "check" all <li> elements of any "checked" <ul> elements...If I were writing code directly. But I'm unfamiliar with how to implement something like this in Drupal. . Do I make templates of both the views exposed filter and the Profile2 edit page (the content is entered by users on the Profile2 profile)? . And if so, how do I determine which template to use... – Christopher Dunning Feb 16 '15 at 12:46
  • Sorry, I hit return and submitted comment before I had a chance to explain that, while the solution is elegant I don't know how to implement it in Drupal. I am still unclear (in general) how to get past the Drupal layer and write the code. If I were making a standalone page with checkboxes, this would be a snap. But it has to work in both Profile2 edit pages and the Views exposed filter... – Christopher Dunning Feb 16 '15 at 12:51
  • You will need a custom module (drupal.org/developing/modules/7) that implements the needed javascript (drupal.org/node/756722) on the desired pages. I'm sorry it is hard to explain how drupal works in a comment but this should get you started. – Fons Vandamme Feb 16 '15 at 13:05
  • No, this is quite helpful, and does get me started. I see comments all the time here saying "write your own module" etc. but in searching I've never found the pages you linked to (not sure why...). – Christopher Dunning Feb 16 '15 at 13:13
  • No problem, I suggest your read up on what I've send you. This will obviously raise more (specific) questions that will make us able to help you further. – Fons Vandamme Feb 16 '15 at 13:15

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.