public function OgBehaviorHandler::validate in Organic groups 7.2
Implements EntityReference_BehaviorHandler_Abstract::validate().
Re-build $errors array to be keyed correctly by "default" and "admin" field modes.
@todo: Try to get the correct delta so we can highlight the invalid reference.
Overrides EntityReference_BehaviorHandler_Abstract::validate
See also
entityreference_field_validate().
File
- plugins/
entityreference/ behavior/ OgBehaviorHandler.class.php, line 249
Class
- OgBehaviorHandler
- OG behavior handler.
Code
public function validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$new_errors = array();
$values = array(
'default' => array(),
'admin' => array(),
);
$item = reset($items);
if (!empty($item['field_mode'])) {
// This is a complex widget with "default" and "admin" field modes.
foreach ($items as $item) {
$values[$item['field_mode']][] = $item['target_id'];
}
}
else {
foreach ($items as $item) {
if (!entityreference_field_is_empty($item, $field) && $item['target_id'] !== NULL) {
$values['default'][] = $item['target_id'];
}
}
}
$field_name = $field['field_name'];
foreach ($values as $field_mode => $ids) {
if (!$ids) {
continue;
}
if ($field_mode == 'admin' && !user_access('administer group')) {
// No need to validate the admin, as the user has no access to it.
continue;
}
$instance['field_mode'] = $field_mode;
$valid_ids = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)
->validateReferencableEntities($ids);
if ($invalid_entities = array_diff($ids, $valid_ids)) {
foreach ($invalid_entities as $id) {
$new_errors[$field_mode][] = array(
'error' => 'og_invalid_entity',
'message' => t('The referenced group (@type: @id) is invalid.', array(
'@type' => $field['settings']['target_type'],
'@id' => $id,
)),
);
}
}
}
if ($new_errors) {
og_field_widget_register_errors($field_name, $new_errors);
// We throw an exception ourself, as we unset the $errors array.
throw new FieldValidationException($new_errors);
}
// Errors for this field now handled, removing from the referenced array.
unset($errors[$field_name]);
}