drupal-webformcivicrm

in a civicrm webform, create multiple 'groups' fields


In CiviCRM webform, you can 'enable tag and groups'. Configuring those allows you to create option elements in the webform.

This creates one 'widget', one dropdown or set of checkboxes. I have two field instances where I want the user to select a group - say for example

a,b,c,d,e and f are all groups. I can not change that.

How could I do that ?


Solution

  • After using Andrew Hunts suggestion for a while, I finally solved this on the server side, in a custom module, using webform logic as described here http://www.webomelette.com/drupal-webform-submission-presave-hook

    Basicly, on presave, I look for 2 custom fields containing group ids (mailing and food in the example). Then I add these to the CiviCRM groups field.

    I'll add the code below, which has some more logic:

    You could probably make it even more generic, but since I had different logic for the different groups, this was suitable for me.

    function getFoodGroups() {
        // return foodgroups
    }
    function getMailGroups() {
        // return mailgroups
    }
    
    function MYMODULE_webform_submission_presave($node, &$submission) {
    
    
    
        $groupselm      = '';
        $groups_cid     = false;
        $foods_cid  = false;
        $mailings_cid = false;
    
        // http://www.webomelette.com/drupal-webform-submission-presave-hook
        foreach($node->webform['components'] as $cid=>$comp) {
            if ($comp['form_key']=='the_groups_element') {
                $groupselm = $comp['value'];
                break;
            }
        }
    
    
        if ($groupselm) {
    
            foreach($node->webform['components'] as $cid=>$comp) {
                if ($comp['form_key']==$groupselm) $groups_cid = $comp['cid'];
                if ($comp['form_key']=='the_foods') $foods_cid = $comp['cid'];
                if ($comp['form_key']=='the_mailings') $mailings_cid = $comp['cid'];
            }
    
            $group_gids = $submission->data[$groups_cid];
            if (!$group_gids) $group_gids=array();
    
    
    
    
            if ($foods_cid!==false && $submission->data[$foods_cid]) {
    
                // remove all current foods
                foreach ($group_gids as $gidx=>$group_gid) {
                    foreach (getFoodGroups() as $foodgroup) {
                        if ($group_gid==$foodgroup['gid']) {
                            if ($debug) drupal_set_message('removing foodgroup '.$foodgroup['gid']);
                            unset($group_gids[$gidx]);
                        }
                    }
                }
    
                // validate and add submitted regions
                $foodsgids = $submission->data[$foods_cid];
                if (!is_array($foodsgids)) $foodsgids = array($foodsgids);
                foreach ($foodsgids as $foodsgid) {
                    foreach (getFoodGroups() as $foodgroup) {
                        if ($foodsgid==$foodgroup['gid']) {
                            $group_gids[]=$foodsgid;
                            break; // only one food allowed
                        }
                    }
                }
            }
    
            if ($mailings_cid!==false && $submission->data[$mailings_cid]) {
    
                // just add submitted mailings, dont remove any
                $mailinggids = $submission->data[$mailings_cid];
                if (!is_array($mailinggids)) $mailinggids = array($mailinggids);
                foreach ($mailinggids as $mailinggid) {
                    foreach (getMailGroups() as $mailing) {
                        if ($mailinggid==$mailing['gid']) {
                            if ($debug) drupal_set_message('adding mailing '.$mailing['gid']);
                            $group_gids[]=$mailinggid;
                        }
                    }
                }
            }
    
            $submission->data[$groups_cid] = array_unique($group_gids);
    
        }