Answers.Body: <p>After digging deeply into webform_civicrm, I've found that the hook to use is <strong>hook_webform_submission_insert</strong></p>
  
  <p>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 <strong>hook_module_implements_alter</strong></p>
  
  <p>I couldn't access contact_id created in any webform_civicrm entities, so I had to get it from the table <strong>webform_civicrm_submissions</strong> where is saved</p>
  
  <p>The source code:</p>
  
  <pre><code>// Be sure our module's hook is the last to be executed
  function mymodule_module_implements_alter(&amp;$implementations, $hook) {
    if ($hook == 'webform_submission_insert') {
      $group = $implementations['mymodule'];
      unset($implementations['mymodule']);
      $implementations['mymodule'] = $group;
    }
  }
  
  function mymodule_webform_submission_insert($node, $submission){
    if (!empty($node-&gt;webform_civicrm)){
      if(... my condition based on $submission values ...){
        $sid = $submission-&gt;sid;
        $result = db_select('webform_civicrm_submissions', 'w')
          -&gt;fields('w', array('contact_id'))
          -&gt;condition('sid', $sid)
          -&gt;execute()
          -&gt;fetchAssoc();
  
        // contact_id is saved between '-'
        if($result['contact_id'])
          $contact_id = str_replace('-', '', $result['contact_id']);
  
        civicrm_api3('GroupContact', 'create', array('group_id' =&gt; $mygroup_id, 'contact_id' =&gt; $contact_id));
      }
    }
  }
  </code></pre>
  
  <hr>
  
  <p>PS: <em>org.civicoop.civiruleswebform</em> looks like a nice alternative, but it has many dependencies (2 extensions and 4 modules need to be installed in Drupal to use it)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-21T15:55:19.050Z
Answers.LastActivityDate: 2017-08-21T15:55:19.050Z
Answers.UserDisplayName: luciano_s
Answers.Body: <p>There are probably lots of solutions but I have created an extension which allows you to use CiviRules and Webform-CiviCRM together: <a href="https://github.com/CiviCooP/org.civicoop.civiruleswebform" rel="nofollow noreferrer">https://github.com/CiviCooP/org.civicoop.civiruleswebform</a>.</p>
  
  <p>That might do the trick for you? You can then add the contact to the group with a CiviRules action.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-21T12:20:31.340Z
Answers.Text: 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 :)
Answers.UserDisplayName: luciano_s
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-21T11:53:22.020Z
Answers.LastActivityDate: 2017-08-21T11:53:22.020Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>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'.</p>
  
  <p>Eg Apple sets to Group = Fruit
  Apple or Oranges sets to = Fruit
  Apple and Carrot sets to = Both
  Carrot sets to = Veg</p>
  
  <p>Of course that is a very simplistic approach but hopefully conveys the possibilities.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-21T19:15:25.653Z
Answers.LastActivityDate: 2017-08-21T19:15:25.653Z
Answers.UserDisplayName: petednz - fuzion
Answers.Body: <p>In Drupal 8 Webform-CiviCRM, this should be easier.  Create an element of type &quot;Calculated Twig&quot; and give it the same key name as the &quot;Groups&quot; 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T20:10:37.847Z
Answers.LastActivityDate: 2021-05-03T20:10:37.847Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I want to add a contact to a group created from a Webform CiviCRM, depending on the fields' values.</p>
  
  <p>I don't find where I should place this logic:</p>
  
  <ul>
  <li>In hook hook_civicrm_post, I don't have the origin (webform) of the contact created.</li>
  <li>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.</li>
  </ul>
  
  <p>Any ideas?</p>
  
  <p>Working with CiviCRM 4.6.30, webform 7.x-4.15, webform_civicrm 7.x-4.18</p>
  
Tags: <webform-civicrm>
Title: Webform CiviCRM, add contact to a group programatically
ThreadText: # Webform CiviCRM, add contact to a group programatically
  
  (Asked by: username_0 on 2017-08-21)
  
  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
  
  **Tags:** webform-civicrm
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2017-08-21. Score: 3
  
  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'];
      unset($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)
          ->execute()
          ->fetchAssoc();
  
        // contact_id is saved between '-'
        if($result['contact_id'])
          $contact_id = str_replace('-', '', $result['contact_id']);
  
        civicrm_api3('GroupContact', 'create', array('group_id' => $mygroup_id, 'contact_id' => $contact_id));
      }
    }
  }
  
  ```
  
  ---
  
  PS: *org.username_1.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)
  
  
  ---
  
  ### Answer by username_1 on 2017-08-21. Score: 2
  
  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/username_1/org.username_1.civiruleswebform>.
  
  That might do the trick for you? You can then add the contact to the group with a CiviRules action.
  
  > **Comments:**
  > **username_0**: thanks @username_1-username_1 ! I wasn't aware of this integration between CiviRules and webform_civicrm, sounds a nice solution I'll give it a try :)  
  
  ---
  
  ### Answer by username_2 on 2017-08-21. Score: 1
  
  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.
  
  
  ---
  
  ### Answer by username_3 on 2021-05-03. Score: 1
  
  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.
  
  
  ---
  
  