1 field.crud.inc | field_validate_instance($instance, $update = FALSE, $load_field = TRUE) |
Validates a field instance.
Parameters
$instance: The instance name to read.
$update: Whether this is a new or existing field instance.
$load_field: Whether the field instance should be read directly from config.
Related topics
File
- core/
modules/ field/ field.crud.inc, line 611 - Field CRUD API, handling field and field instance creation and deletion.
Code
function field_validate_instance($instance, $update = FALSE, $load_field = TRUE) {
$label = isset($instance['label']) ? $instance['label'] : $instance['field_name'];
// Check that the required properties exists.
if (empty($instance['entity_type'])) {
throw new FieldException(t('The field instance "@label" does not specify an entity type.', array('@label' => $label)));
}
if (empty($instance['bundle'])) {
throw new FieldException(t('The field instance "@label" does not specify a bundle type.', array('@label' => $label)));
}
if ($load_field) {
$field = field_read_field($instance['field_name']);
if (empty($field)) {
throw new FieldException(t('The field instance "@label" cannot be saved because the field "@name" does not exist.', array('@label' => $label, '@name' => $instance['field_name'])));
}
// Check that the field can be attached to this entity type.
if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) {
throw new FieldException(t('The field instance "@label" on the type "@entity_type" is not allowed.', array('@label' => $label, '@entity_type' => $instance['entity_type'])));
}
// Check to ensure that the field instance does not have a reserved word for
// this entity type.
$entity_info = entity_get_info($instance['entity_type']);
if (in_array($instance['field_name'], $entity_info['entity keys'])) {
// If it does, check whether this is new or an update and tailor the
// message accordingly.
$error_message_placeholders = array(
'@name' => $instance['field_name'],
'@type' => $entity_info['label'],
);
if ($update) {
$error_message = t("The machine name '@name' is invalid for entity type @type as this machine name is reserved by this entity type. You will not be able to save any @type with this field on. You should delete this field and create a new field with a different machine name", $error_message_placeholders);
}
else {
$error_message = t("You cannot add the existing field with the machine name '@name' to entity type @type as this machine name is reserved by this entity type", $error_message_placeholders);
}
throw new FieldException($error_message);
}
// @todo: Expand the check for reserved words to all properties of the entity type.
// @todo: Check that the widget type is known and can handle the field type.
// @todo: Check that the formatters are known and can handle the field type.
// @todo: Check that the display display modes are known for the entity type.
}
}