You are here

function mass_contact_taxonomy_recipients in Mass Contact 7

Retrieves a list of users by taxonomy terms.

Gets the user IDs for all users that have the specified taxonomy terms attached to user object.

Parameters

array $recipients: The list of items for this category. For this plugin implementation, it is an array of taxonomy term IDs.

Return value

null|array The user IDs that are part of specified taxonomy terms.

1 string reference to 'mass_contact_taxonomy_recipients'
mass_contact_taxonomy.inc in plugins/mass_contact_taxonomy.inc

File

plugins/mass_contact_taxonomy.inc, line 42

Code

function mass_contact_taxonomy_recipients(array $recipients) {

  // Check to see if a taxonomy term has been selected.
  if (!isset($recipients['mass_contact_taxonomy']) || empty($recipients['mass_contact_taxonomy'])) {
    return;
  }

  // Check all fields of user entity and if field is taxonomy term reference,
  // add condition.
  $user_fields = field_info_instances('user');
  if (empty($user_fields)) {
    return;
  }
  $uids = array();

  // Iterate through all the fields attached to the user entity.
  foreach ($user_fields['user'] as $user_field_instance) {

    // Get the field's information.
    $field = field_info_field($user_field_instance['field_name']);

    // EntityFieldQuery() cannot handle OR logic, so for each possible taxonomy
    // term attached to a user, we make a separate query.
    if (isset($field['module']) && $field['module'] == 'taxonomy') {

      // Create a query to select users by taxonomy term.
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'user');
      $query
        ->fieldCondition($field['field_name'], 'tid', $recipients['mass_contact_taxonomy'], 'IN');
      $result = $query
        ->execute();

      // Collect uids.
      if (isset($result['user'])) {

        // Save out the keys from the result set.
        $uids = array_keys($result['user']);
      }
    }
  }
  return $uids;
}