| Versionen | |
|---|---|
| drupal7 | field_create_field($field) |
Create a field.
This function does not bind the field to any bundle; use field_create_instance() for that.
@throw FieldException
$field A field structure. The field_name and type properties are required. Other properties, if omitted, will be given the following default values:
modules/
<?php
function field_create_field($field) {
// Field name is required.
if (empty($field['field_name'])) {
throw new FieldException('Attempt to create an unnamed field.');
}
// Field type is required.
if (empty($field['type'])) {
throw new FieldException('Attempt to create a field with no type.');
}
// Field name cannot contain invalid characters.
if (preg_match('/[^a-z0-9_]/', $field['field_name'])) {
throw new FieldException('Attempt to create a field with invalid characters. Only alphanumeric characters and underscores are allowed.');
}
// TODO: check that field_name < 32 chars.
// Check that the field type is known.
$field_type = field_info_field_types($field['type']);
if (!$field_type) {
throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $field['type'])));
}
// Ensure the field name is unique. We also check disabled or deleted fields.
// TODO : do we want specific messages when clashing with a disabled or inactive field ?
$prior_field = field_read_field($field['field_name'], array('include_inactive' => TRUE, 'include_deleted' => TRUE));
if (!empty($prior_field)) {
throw new FieldException(t('Attempt to create field name %name which already exists.', array('%name' => $field['field_name'])));
}
$field += array(
'cardinality' => 1,
'locked' => FALSE,
'settings' => array(),
);
// Create all per-field-type properties (needed here as long as we have
// settings that impact column definitions).
$field['settings'] += field_info_field_settings($field['type']);
$field['module'] = $field_type['module'];
$field['active'] = 1;
$field['deleted'] = 0;
// Collect storage information.
$schema = (array) module_invoke($field['module'], 'field_schema', $field);
$schema += array('columns' => array(), 'indexes' => array());
// 'columns' are hardcoded in the field type.
$field['columns'] = $schema['columns'];
// 'indexes' can be both hardcoded in the field type, and specified in the
// incoming $field definition.
$field += array(
'indexes' => array(),
);
$field['indexes'] += $schema['indexes'];
// Inform the storage engine.
module_invoke(variable_get('field_storage_module', 'field_sql_storage'), 'field_storage_create_field', $field);
// The serialized 'data' column contains everything from $field that does not
// have its own column and is not automatically populated when the field is
// read.
$data = $field;
unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['active'], $data['deleted']);
$field['data'] = $data;
drupal_write_record('field_config', $field);
// Clear caches
field_cache_clear(TRUE);
}
?>
Kommentare
Kommentar hinzufügen