You are here

function node_gallery_api_create_field in Node Gallery 7

Create a node gallery field in a bundle.

Parameters

string $field_name: The field name

string $entity_type: The entity type

string $bundle: The bundle name.

string $ng_field: Optional; Array with field definitions, to allow easier overriding by the caller. If empty, function will get the field info by calling node_gallery_api_fields_info() with the field name.

1 call to node_gallery_api_create_field()
node_gallery_api_entity_update in ./node_gallery_api.module
Implements hook_entity_update().

File

./node_gallery_api.module, line 1271
Node Gallery module.

Code

function node_gallery_api_create_field($field_name, $entity_type, $bundle, $ng_field = array()) {
  if (empty($ng_field)) {
    $ng_field = node_gallery_api_field_information($field_name);
  }
  $field = field_info_field($field_name);

  // Allow overriding the field name.
  $ng_field['field']['field_name'] = $field_name;
  if (empty($field)) {
    $field = field_create_field($ng_field['field']);
  }
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($instance)) {
    if (!empty($ng_field['instance'])) {
      $instance = $ng_field['instance'];
    }
    elseif (!empty($field['bundles']['node'][0])) {
      $copy_from_bundle = $field['bundles']['node'][0];
      $instance = field_info_instance($entity_type, $field_name, $copy_from_bundle);
    }
    else {
      $instance = array();
    }
    $instance['field_name'] = $field_name;
    $instance['bundle'] = $bundle;
    $instance['entity_type'] = $entity_type;
    field_create_instance($instance);
  }
}