function tripal_create_bundle_fields

3.x tripal.entities.api.inc tripal_create_bundle_fields($bundle, $term)

Refreshes the bundle such that new fields added by modules will be found.

Parameters

$bundle_name: The name of the bundle to refresh (e.g. bio_data_4).

Return value

The array of field instance names that were added.

Related topics

2 calls to tripal_create_bundle_fields()
tripal_check_new_fields in tripal/tripal.module
tripal_create_bundle in tripal/api/tripal.entities.api.inc
Creates a new Tripal Entity type (i.e. bundle).

File

tripal/api/tripal.entities.api.inc, line 637
Provides an application programming interface (API) for working with TripalEntity content types (bundles) and their entities.

Code

function tripal_create_bundle_fields($bundle, $term) {

  $added = array();

  // Allow modules to add fields to the new bundle.
  $modules = module_implements('bundle_fields_info');
  $info = array();
  foreach ($modules as $module) {
    $function = $module . '_bundle_fields_info';
    $temp = $function('TripalEntity', $bundle);
    $info = array_merge($info, $temp);
  }

  // Allow modules to alter which fields should be attached to content
  // types they create.
  drupal_alter('bundle_fields_info', $info, $bundle, $term);

  // Iterate through all of the fields and create them.
  foreach ($info as $field_name => $details) {
    $field_type = $details['type'];

    // TODO: make sure the field term exits. If not then
    // skip it.

    // If the field already exists then skip it.
    $field = field_info_field($details['field_name']);
    if ($field) {
      continue;
    }

    // Create the field.
    $field = field_create_field($details);
    if (!$field) {
      tripal_set_message(t("Could not create new field: %field.", 
      array('%field' => $details['field_name'])), TRIPAL_ERROR);
    }
  }

  // Allow modules to add instances to the new bundle.
  $modules = module_implements('bundle_instances_info');
  $info = array();
  foreach ($modules as $module) {
    $function = $module . '_bundle_instances_info';
    $temp = $function('TripalEntity', $bundle);
    $info = array_merge($info, $temp);
  }

  // Allow modules to alter which fields should be attached to content
  // types they create.
  drupal_alter('bundle_instances_info', $info, $bundle, $term);

  // Iterate through all of the field instances and create them.
  foreach ($info as $field_name => $details) {

    // If the field is already attached to this bundle then skip it.
    $field = field_info_field($details['field_name']);
    if ($field and array_key_exists('bundles', $field) and 
      array_key_exists('TripalEntity', $field['bundles']) and 
      in_array($bundle->name, $field['bundles']['TripalEntity'])) {
      continue;
    }

    // Create the field instance.
    $instance = field_create_instance($details);
    $added[] = $field_name;
  }
  return $added;
}