You are here

function blockreference_field_validate in Block reference 7

Implements hook_field_validate().

Possible error codes:

  • 'invalid_bid': bid is not valid for the field (not a valid block id, or the block is not referenceable).

File

./blockreference.module, line 202
Defines a field type for referencing a block from a node.

Code

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

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

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

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