function node_reference_field_validate in References 7.2
Implements hook_field_validate().
Possible error codes:
- 'invalid_nid': nid is not valid for the field (not a valid node id, or the
node is not referenceable).
File
- node_reference/
node_reference.module, line 162 - Defines a field type for referencing one node from another.
Code
function node_reference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// Extract nids to check.
$ids = array();
// First check non-numeric "nid's to avoid losing time with them.
foreach ($items as $delta => $item) {
if (is_array($item) && !empty($item['nid'])) {
if (is_numeric($item['nid'])) {
$ids[] = $item['nid'];
}
else {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_nid',
'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 = node_reference_potential_references($field, $options);
foreach ($items as $delta => $item) {
if (is_array($item)) {
if (!empty($item['nid']) && !isset($refs[$item['nid']])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_nid',
'message' => t("%name: this post can't be referenced.", array(
'%name' => $instance['label'],
)),
);
}
}
}
}
}