You are here

function rolereference_field_validate in Role Reference 7

Implements hook_field_validate().

Possible error codes:

  • 'invalid_rid': nid is not valid for the field (not a valid role id, or the role is not referenceable).

File

./rolereference.module, line 68

Code

function rolereference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {

  // Extract nids to check.
  $ids = array();

  // First check non-numeric rid's to avoid losing time with them.
  foreach ($items as $delta => $item) {
    if (is_array($item) && !empty($item['rid'])) {
      if (is_numeric($item['rid'])) {
        $ids[] = $item['rid'];
      }
      else {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'invalid_rid',
          'message' => t("%name: invalid input.", array(
            '%name' => $instance['label'],
          )),
        );
      }
    }
  }

  // Prevent performance hog if there are no ids to check.
  if ($ids) {
    $referenceable_roles = rolereference_potential_references($field['settings']['referenceable_roles'], $field['field_name']);
    foreach ($items as $delta => $item) {
      if (is_array($item)) {
        if (!empty($item['rid']) && !isset($referenceable_roles[$item['rid']])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'invalid_rid',
            'message' => t("%name: this role can't be referenced.", array(
              '%name' => $instance['label'],
            )),
          );
        }
      }
    }
  }
}