field_create_instance

Versions
7
field_create_instance($instance)

Creates an instance of a field, binding it to a bundle.

throw FieldException

Parameters

$instance A field instance structure. The field_name, object_type and bundle properties are required. Other properties, if omitted, will be given the following default values:

  • label: the field name
  • description: empty string
  • weight: 0
  • required: FALSE
  • default_value_function: empty string
  • settings: each omitted setting is given the default value specified in hook_field_info().
  • widget:
  • display: Settings for the 'full' build mode will be added, and each build mode will be completed with the follwong default values:

Return value

The $instance structure with the id property filled in.

Related topics

▾ 5 functions call field_create_instance()

default_install in profiles/default/default.install
Implement hook_install().
field_ui_field_overview_form_submit in modules/field_ui/field_ui.admin.inc
Submit handler for the field overview form.
forum_enable in modules/forum/forum.install
node_configure_fields in modules/node/node.module
Manage the field(s) for a node type.
taxonomy_update_7004 in modules/taxonomy/taxonomy.install
Move taxonomy vocabulary associations for nodes to fields and field instances.

Code

modules/field/field.crud.inc, line 601

<?php
function field_create_instance($instance) {
  $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 or is currently inactive.");
  }
  // Check that the required properties exists.
  if (empty($instance['object_type'])) {
    throw new FieldException(t('Attempt to create an instance of field @field_name without an object type.', array('@field_name' => $instance['field_name'])));
  }
  if (empty($instance['bundle'])) {
    throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name'])));
  }

  // Set the field id.
  $instance['field_id'] = $field['id'];

  // Note that we do *not* prevent creating a field on non-existing bundles,
  // because that would break the 'Body as field' upgrade for contrib
  // node types.

  // 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 within the bundle.
  // We only check for instances of active fields, since adding an instance of
  // a disabled field is not supported.
  $prior_instance = field_read_instance($instance['object_type'], $instance['field_name'], $instance['bundle']);
  if (!empty($prior_instance)) {
    $message = t('Attempt to create a field instance that already exists.');
    throw new FieldException($message);
  }

  _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 $instance;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.