function commerce_product_reference_field_validate in Commerce Core 7
Implements hook_field_validate().
Possible error codes:
- 'invalid_product_id': product_id is not valid for the field (not a valid product id, or the product is not referenceable).
File
- modules/
product_reference/ commerce_product_reference.module, line 636 - Defines a field type for referencing products from other entities.
Code
function commerce_product_reference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$translated_instance = commerce_i18n_object('field_instance', $instance);
// Extract product_ids to check.
$product_ids = array();
// First check non-numeric product_id's to avoid losing time with them.
foreach ($items as $delta => $item) {
if (is_array($item) && !empty($item['product_id'])) {
if (is_numeric($item['product_id'])) {
$product_ids[] = $item['product_id'];
}
else {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_product_id',
'message' => t('%name: you have specified an invalid product for this reference field.', array(
'%name' => $translated_instance['label'],
)),
);
}
}
}
// Prevent performance hog if there are no ids to check.
if ($product_ids) {
$refs = commerce_product_match_products($field, $instance, '', NULL, $product_ids);
foreach ($items as $delta => $item) {
if (is_array($item)) {
if (!empty($item['product_id']) && !isset($refs[$item['product_id']])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_product_id',
'message' => t('%name: you have specified an invalid product for this reference field.', array(
'%name' => $translated_instance['label'],
)),
);
}
}
}
}
}