1 field.crud.inc | field_validate_field($field, $update = FALSE) |
Validates a field configuration.
Parameters
$field: A field structure. $field['field_name'] and $field['type'] must be provided.
$update: Whether this is an existing field or a new one.
Related topics
File
- core/
modules/ field/ field.crud.inc, line 249 - Field CRUD API, handling field and field instance creation and deletion.
Code
function field_validate_field($field, $update = FALSE) {
// Field name is required.
if (empty($field['field_name'])) {
throw new FieldException(format_string('Attempt to validate a field with no field name specified.'));
}
// Field type is required.
if (empty($field['type'])) {
throw new FieldException(format_string('The field "@name" did not specify a field type.', array('@name' => $field['field_name'])));
}
// Field name cannot contain invalid characters.
if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $field['field_name'])) {
throw new FieldException(format_string('The field @field_name contains invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character', array('@field_name' => $field['field_name'])));
}
// Validations that only apply to newly created fields.
if (!$update) {
// Field module must exist when creating a field (but not updating it).
$field_info = field_info_field_types($field['type']);
if (!module_exists($field_info['module'])) {
throw new FieldException(t('The field "@name" could not be created because it requires the "@module" module.', array('@name' => $field['field_name'], '@module' => $field_info['module'])));
}
// Ensure the table namespaces are available.
if (db_table_exists('field_data_' . $field['field_name'])) {
throw new FieldException(t('The field "@name" could not be created because a database table with the name "@table" already exists.', array('@name' => $field['field_name'], '@table' => 'field_data_' . $field['field_name'])));
}
if (db_table_exists('field_revision_' . $field['field_name'])) {
throw new FieldException(t('The field "@name" could not be created because a database table with the name "@table" already exists.', array('@name' => $field['field_name'], '@table' => 'field_revision_' . $field['field_name'])));
}
// Ensure the field name is unique.
$prior_field = field_read_field($field['field_name'], array('include_inactive' => TRUE, 'include_deleted' => TRUE));
if (!empty($prior_field)) {
$message = $prior_field['active'] ?
t('The field "@name" already exists.', array('@name' => $field['field_name'])) :
t('The field "@name" already exists, but it is inactive. To create a field with the same name, the configuration and data from the old field must be manually deleted.', array('@name' => $field['field_name']));
throw new FieldException($message);
}
// Field name cannot be longer than 32 characters. We use backdrop_strlen()
// because the DB layer assumes that column widths are given in characters,
// not bytes.
if (backdrop_strlen($field['field_name']) > 32) {
throw new FieldException(t('The field "@name" could not be created because the field name may be no longer than 32 characters.', array('@name' => $field['field_name'])));
}
// Disallow reserved field names. This can't prevent all field name
// collisions with existing entity properties, but some is better
// than none.
foreach (entity_get_info() as $type => $info) {
if (in_array($field['field_name'], $info['entity keys'])) {
throw new FieldException(t('Cannot create a field with the name "@name", which is reserved by entity type @type.', array('@name' => $field['field_name'], '@type' => $type)));
}
}
}
}