# CiviGroup Roles Sync - workaround for Smart Groups?

Has anyone implemented any workarounds to use a Smart Group as the basis for a CiviGroup Roles Sync with Drupal?

I understand that Smart Groups don't work (only contacts added manually to the smart group will be picked up) but I'm wondering if anybody has ever tried any sort of cron + CSV chicanery to mirror a smart group in a manually added group, or anything else to get around the issue?

We're a small org trying to cater for a large (and providing we can make it work automatically, much larger) audience with varying membership, relationship and access requirements, so I need a really easy and automatic way to get this done.

• Maybe Rules or CiviRules is the way to go on this? – Graham Sep 28 '16 at 12:36

Check out my answer to a similar questions: Syncing Drupal Roles from CiviCRM Smart Groups

I think this is basically the same thing....

I just had to resolve this issue, but upon re-reading your question, I don't think my answer will work as well for you.

First - cron + CSV chicanery SHOULD work. The issue is that when users are added to a smart group, it doesn't fire the civicrm_post hook (it's not really feasible performance-wise). However, they ARE fired on import.

I'd use the command-line export tool, something like:

php <civiroot>/bin/csv/export -e GroupContact group_id=<smart group ID> --file /tmp/export.csv
php <civiroot>/bin/csv/import -e GroupContact --file /temp/export/csv


In my solution, all the smart groups were created based on the value of a single custom field. So I wrote a quick extension that added people to groups based on the value of the custom field. You may be able to use CiviRules for this, I'm not 100% sure. Here's that code:

// Fire when the "Role Info" group is edited.
function primaryrolesync_civicrm_custom($op,$customGroupID, $entityID, &$params) {
// Hard-coded: The Role Info group ID is 30.  The custom field ID is 152.
if ($customGroupID == 30) { // Get the role name from the custom field value.$result = civicrm_api3('Contact', 'getoptions', ['field' => "custom_152"]);
$roleName =$result['values'][$params[0]['value']]; // Get the group ID that corresponds to this primary role.$result = civicrm_api3('Group', 'get', [
'title' => $roleName, 'sequential' => 1, 'return' => 'id', ]);$groupId = $result['id']; // Add this contact to the appropriate group. if ($groupId) {
$result = civicrm_api3('GroupContact', 'create', [ 'group_id' =>$groupId,
'contact_id' => \$entityID,
]);
}
}
}


I will understand if this needs down voting as an answer as i have no idea how big a leap it would take to make use of what i will describe - but in case it useful ...

there is some code here that helps deal with Smart Group issues and Views, which is explained in an answer here.

maybe you could use the table it constructs for your own purposes

• not OP but I thought this smart group + views was really cool so I tried this out of curiosity and it worked for me pretty much "out-of-the-box" (not extensively tested)! I installed your code and set up a view of contacts in a smart group, added a VBO field (the VBO itself doesn't work quite right but that doesn't matter too much here), loaded entity objects via VBO in rules, looped over the objects loading drupal users, check for role, and adding a role if dne. Presumably this rule could run on cron and it might just work. Might take a bit more shoehorning though... Cool! – Eli Lisseck Feb 17 '17 at 20:00
• Thx Eli - did you go any further with this – petednz - fuzion May 9 '18 at 22:06

If the 'characteristics' of who gets the role is reasonably simple, then Drupal Rules would be worth exploring further, esp perhaps with CiviCRM Entity in the mix