+ // sudoman hack begins
+ //
+ // This hack allows us to filter Groups listings so users aren't
+ // overwhelmed with choices. The advantage of this over smart groups
+ // is that normal groups use double opt-in.
+ //
+ // To set the variable, use the following method:
+ //
+ // $ drush vset --format=string groups_field_filters "{'profile': {'468': [25, 41]}, 'contribution': {'14': [25, 41]}, 'event': {'49': [25, 41]}}"
+ //
+ // The "string" format must be used and not the "json" format.
+ // Otherwise the json will be converted to a PHP array, which will not
+ // work with this code.
+ //
+ // The string contains json text, with the first level referring to
+ // page type, then the page number, then the list of ids of groups to
+ // display on that page.
+ //
+ // 'profile' is for standalone profiles. If the profile is embedded in
+ // a contrib or event page, then the id of the contrib or event page
+ // must be added to the string.
+
+ // Find the list of groups that we want to let pass through a filter on
+ // a given profile or page. If there is no filter for that page, then
+ // all groups are displayed.
+
+ $groups_field_filters = json_decode(variable_get('groups_field_filters', NULL), $assoc = TRUE);
+
+ $form_class_name = get_class($form);
+
+ // this commented line is useful for printing class names for future use in the switch statement below:
+ //watchdog("hacking", "foo message (form class): " . $form_class_name, NULL, WATCHDOG_ALERT);
+
+ switch ($form_class_name) {
+
+ case 'CRM_Profile_Form_Edit':
+ $page_id = $form->get('gid'); // gid, not id
+ $filter = $groups_field_filters['profile'][strval($page_id)];
+ break;
+
+ case 'CRM_Contribute_Form_Contribution_Main':
+ case 'CRM_Contribute_Form_Contribution_Confirm':
+ case 'CRM_Contribute_Form_Contribution_ThankYou':
+ $page_id = $form->get('id'); // id, not gid
+ $filter = $groups_field_filters['contribution'][strval($page_id)];
+ break;
+
+ case 'CRM_Event_Form_Registration_Register':
+ $page_id = $form->get('id'); // id, not gid
+ $filter = $groups_field_filters['event'][strval($page_id)];
+ break;
+ }
+ // sudoman hack continues further below
+