| Versionen | |
|---|---|
| drupal7 | field_create_instance($instance) |
Creates an instance of a field, binding it to a bundle.
@throw FieldException
$instance A field instance structure. The field_name and bundle properties are required. Other properties, if omitted, will be given the following default values:
modules/
<?php
function field_create_instance($instance) {
// Check that the specified field exists.
$field = field_read_field($instance['field_name']);
if (empty($field)) {
throw new FieldException("Attempt to create an instance of a field that doesn't exist.");
}
// Set the field id.
$instance['field_id'] = $field['id'];
// TODO: Check that the specifed bundle exists.
// 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 build modes are known for the object type ?
// Those checks should probably happen in _field_write_instance() ?
// Problem : this would mean that a UI module cannot update an instance with a disabled formatter.
// Ensure the field instance is unique.
// TODO : do we want specific messages when clashing with a disabled or inactive instance ?
$prior_instance = field_read_instance($instance['field_name'], $instance['bundle'], array('include_inactive' => TRUE, 'include_deleted' => TRUE));
if (!empty($prior_instance)) {
throw new FieldException('Attempt to create a field instance which already exists.');
}
_field_write_instance($instance);
// Clear caches
field_cache_clear();
// Invoke external hooks after the cache is cleared for API consistency.
module_invoke_all('field_create_instance', $instance);
return FALSE;
}
?>
Kommentare
Kommentar hinzufügen