I want to add a contact to a group created from a Webform CiviCRM, depending on the fields' values.

I don't find where I should place this logic:

  • In hook hook_civicrm_post, I don't have the origin (webform) of the contact created.
  • The place should be in Drupal / Webform hooks, but I don't know how to "inject" the group to be assigned to the contact, to be processed by webform_civicrm.

Any ideas?

Working with CiviCRM 4.6.30, webform 7.x-4.15, webform_civicrm 7.x-4.18

4 Answers 4


After digging deeply into webform_civicrm, I've found that the hook to use is hook_webform_submission_insert

It's important to check that this hook is executed after webform_civicrm's one, so the contact is created first and then we can perform our own actions. We can alter execution order with hook_module_implements_alter

I couldn't access contact_id created in any webform_civicrm entities, so I had to get it from the table webform_civicrm_submissions where is saved

The source code:

// Be sure our module's hook is the last to be executed
function mymodule_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'webform_submission_insert') {
    $group = $implementations['mymodule'];
    $implementations['mymodule'] = $group;

function mymodule_webform_submission_insert($node, $submission){
  if (!empty($node->webform_civicrm)){
    if(... my condition based on $submission values ...){
      $sid = $submission->sid;
      $result = db_select('webform_civicrm_submissions', 'w')
        ->fields('w', array('contact_id'))
        ->condition('sid', $sid)

      // contact_id is saved between '-'
        $contact_id = str_replace('-', '', $result['contact_id']);

      civicrm_api3('GroupContact', 'create', array('group_id' => $mygroup_id, 'contact_id' => $contact_id));

PS: org.civicoop.civiruleswebform looks like a nice alternative, but it has many dependencies (2 extensions and 4 modules need to be installed in Drupal to use it)


There are probably lots of solutions but I have created an extension which allows you to use CiviRules and Webform-CiviCRM together: https://github.com/CiviCooP/org.civicoop.civiruleswebform.

That might do the trick for you? You can then add the contact to the group with a CiviRules action.

  • thanks @erikh-civicoop ! I wasn't aware of this integration between CiviRules and webform_civicrm, sounds a nice solution I'll give it a try :)
    – luciano_s
    Aug 21, 2017 at 12:20

Another approach is to use webform Conditionals which are part of webform. For example, if you had a custom field (Fruit and Veg) with options Apple or Orange or Carrot, then you could set your Group field (Fruit, Veg or Both) to be hidden and set the value using Conditionals based on the choice made in 'Fruit'.

Eg Apple sets to Group = Fruit Apple or Oranges sets to = Fruit Apple and Carrot sets to = Both Carrot sets to = Veg

Of course that is a very simplistic approach but hopefully conveys the possibilities.


In Drupal 8 Webform-CiviCRM, this should be easier. Create an element of type "Calculated Twig" and give it the same key name as the "Groups" field. Now you can use Twig calculations based on the rest of the form's values to fill in the Groups field as you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.