Answers.Body: <p>Check out my answer to a similar questions:
  <a href="https://civicrm.stackexchange.com/questions/17710/syncing-drupal-roles-from-civicrm-smart-groups/19523#19523">Syncing Drupal Roles from CiviCRM Smart Groups</a></p>
  
  <p>I think this is basically the same thing....</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-18T02:01:24.457Z
Answers.LastActivityDate: 2017-08-18T02:01:24.457Z
Answers.UserDisplayName: jackrabbithanna
Answers.Body: <p>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</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T20:34:40.437Z
Answers.LastActivityDate: 2017-05-18T20:34:40.437Z
Answers.UserDisplayName: petednz - fuzion
Answers.Body: <p>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 ...</p>
  
  <p>there is some code <a href="https://github.com/fuzionnz/civicrm_views_smartgroup" rel="nofollow noreferrer">here</a> that helps deal with Smart Group issues and Views, which is explained in an answer <a href="https://civicrm.stackexchange.com/questions/11200/can-drupal-views-filter-contacts-based-on-smart-groups">here</a>.</p>
  
  <p>maybe you could use the table it constructs for your own purposes</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-17T20:00:13.553Z
Answers.Text: 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!
Answers.UserDisplayName: Eli Lisseck
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-09T22:06:15.903Z
Answers.Text: Thx Eli - did you go any further with this
Answers.UserDisplayName: petednz - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-18T21:41:26.697Z
Answers.LastActivityDate: 2016-12-18T21:41:26.697Z
Answers.LastEditDate: 2017-04-13T12:53:13.167Z
Answers.UserDisplayName: petednz - fuzion
Answers.Body: <p>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.</p>
  
  <p>First - cron + CSV chicanery SHOULD work.  The issue is that when users are added to a smart group, it doesn't fire the <code>civicrm_post</code> hook (it's not really feasible performance-wise).  However, they ARE fired on import.</p>
  
  <p>I'd use the command-line export tool, something like:</p>
  
  <pre><code>php &lt;civiroot&gt;/bin/csv/export -e GroupContact group_id=&lt;smart group ID&gt; --file /tmp/export.csv
  php &lt;civiroot&gt;/bin/csv/import -e GroupContact --file /temp/export/csv
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>// Fire when the "Role Info" group is edited.
  function primaryrolesync_civicrm_custom($op, $customGroupID, $entityID, &amp;$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' =&gt; "custom_152"]);
      $roleName = $result['values'][$params[0]['value']];
      // Get the group ID that corresponds to this primary role.
      $result = civicrm_api3('Group', 'get', [
        'title' =&gt; $roleName,
        'sequential' =&gt; 1,
        'return' =&gt; 'id',
      ]);
      $groupId = $result['id'];
      // Add this contact to the appropriate group.
      if ($groupId) {
        $result = civicrm_api3('GroupContact', 'create', [
          'group_id' =&gt; $groupId,
          'contact_id' =&gt; $entityID,
        ]);
      }
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-19T20:03:46.660Z
Answers.LastActivityDate: 2016-10-19T20:03:46.660Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>Has anyone implemented any workarounds to use a Smart Group as the basis for a CiviGroup Roles Sync with Drupal?</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-09-28T12:36:33.657Z
Comments.Text: Maybe Rules or CiviRules is the way to go on this?
Comments.UserDisplayName: Graham
Tags: <drupal><relationships>
Title: CiviGroup Roles Sync - workaround for Smart Groups?
ThreadText: # CiviGroup Roles Sync - workaround for Smart Groups?
  
  (Asked by: username_0 on 2016-09-13)
  
  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.
  
  **Tags:** drupal, relationships
  
  > **Comments:**
  > **username_1**: Maybe Rules or CiviRules is the way to go on this?  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2017-08-18. Score: 2
  
  Check out my answer to a similar questions:
  [Syncing Drupal Roles from CiviCRM Smart Groups](https://civicrm.stackexchange.com/questions/17710/syncing-drupal-roles-from-civicrm-smart-groups/19523#19523)
  
  I think this is basically the same thing....
  
  
  ---
  
  ### Answer by username_3 on 2017-05-18. Score: 0
  
  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
  
  
  ---
  
  ### Answer by username_3 on 2016-12-18. Score: 0
  
  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](https://github.com/fuzionnz/civicrm_views_smartgroup) that helps deal with Smart Group issues and Views, which is explained in an answer [here](https://civicrm.stackexchange.com/questions/11200/can-drupal-views-filter-contacts-based-on-smart-groups).
  
  maybe you could use the table it constructs for your own purposes
  
  > **Comments:**
  > **username_4**: 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!  
  > **username_3**: Thx Eli - did you go any further with this  
  
  ---
  
  ### Answer by username_5 on 2016-10-19. Score: 0
  
  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,
        ]);
      }
    }
  }
  
  ```
  
  
  ---
  
  