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.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)