You are here

function redhen_fields_email_in_use in RedHen CRM 7

Helper to ensure an email isn't already in use for a given entity type.

Parameters

array $field: Field instance.

string $entity_type: Entity type.

object $entity: Fully loaded entity object.

mixed $value: Field value.

Return value

bool Whether or not the field value is in use.

1 call to redhen_fields_email_in_use()
redhen_fields_field_validate in modules/redhen_fields/redhen_fields.module
Implements hook_field_validate().

File

modules/redhen_fields/redhen_fields.module, line 356
Defines email, phone and address field types for RedHen CRM.

Code

function redhen_fields_email_in_use($field, $entity_type, $entity, $value) {
  if ($value) {
    list($entity_id, , $bundle) = entity_extract_ids($entity_type, $entity);
    $user_email_type = variable_get(REDHEN_CONTACT_USER_EMAIL_TYPE, array());
    if ($user_email_type && !in_array($bundle, $user_email_type)) {
      return FALSE;
    }
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type, '=');
    if ($entity_id) {
      $query
        ->entityCondition('entity_id', $entity_id, '!=');
    }
    $query
      ->fieldCondition($field['field_name'], 'value', $value);
    $query
      ->range(0, 1);
    if ($user_email_type) {
      $query
        ->entityCondition('bundle', $user_email_type);
    }
    $return = $query
      ->execute();
    return !empty($return);
  }
  return FALSE;
}