You are here

function flipping_book_reference_field_validate in Flipping Book 7

Implements hook_field_validate().

Possible error codes:

  • 'invalid_fbid': fbid is not valid for the field

(not a valid flipping_book id, or the flipping_book is not referenceable).

File

./flipping_book_reference.module, line 68
Defines a field type for referencing one flipping_book from a node.

Code

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

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

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

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