function commerce_line_item_field_validate in Commerce Core 7
Implements hook_field_validate().
Possible error codes:
- 'invalid_line_item_id': line_item_id is not valid for the field (not a valid line item ID).
File
- modules/
line_item/ commerce_line_item.module, line 819 - Defines the core Commerce line item entity and API functions interact with line items on orders.
Code
function commerce_line_item_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$translated_instance = commerce_i18n_object('field_instance', $instance);
// Extract line_item_ids to check.
$line_item_ids = array();
// First check non-numeric line_item_id's to avoid losing time with them.
foreach ($items as $delta => $item) {
if (is_array($item) && !empty($item['line_item_id'])) {
if (is_numeric($item['line_item_id'])) {
$line_item_ids[] = $item['line_item_id'];
}
else {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_line_item_id',
'message' => t('%name: you have specified an invalid line item for this field.', array(
'%name' => $translated_instance['label'],
)),
);
}
}
}
// Prevent performance hog if there are no ids to check.
if ($line_item_ids) {
$line_items = commerce_line_item_load_multiple($line_item_ids);
foreach ($items as $delta => $item) {
if (is_array($item)) {
if (!empty($item['line_item_id']) && !isset($line_items[$item['line_item_id']])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_line_item_id',
'message' => t('%name: you have specified an invalid line item for this reference field.', array(
'%name' => $translated_instance['label'],
)),
);
}
}
}
}
}