function tripal_create_bundle

3.x tripal.entities.api.inc tripal_create_bundle($args, &$error = '')

Creates a new Tripal Entity type (i.e. bundle).

Parameters

$args: An array of arguments that must include the following keys:

  • vocabulary: The abbreviated vocabulary for the vocabulary (e.g. RO, SO, PATO).
  • accession: The unique term ID in the vocabulary $vocabulary (i.e. an accession).
  • term_name: A human-readable name for this term. This will became the name that appears for the content type. In practice, this should be the name of the term. (E.g. the name for SO:0000704 is gene).

$error: A string, passed by reference, that is filled with the error message if the function fails.

Return value

The bundle object or FALSE if failure.

Related topics

5 calls to tripal_create_bundle()
chado_migrate_tripal_content_type in tripal_chado/api/tripal_chado.migrate.api.inc
Migrate Tripal content types
tripal_admin_add_type_form_submit in tripal/includes/TripalBundleUIController.inc
Implements hook_submit() for the tripal_admin_add_type_form.
tripal_chado_migrate_selected_types in tripal_chado/includes/tripal_chado.migrate.inc
Migrate only selected Tripal v2 content types
tripal_chado_prepare_chado in tripal_chado/includes/setup/tripal_chado.setup.inc
Prepares Chado for use by Tripal.
tripal_chado_update_7320 in tripal_chado/tripal_chado.install
Adding Phylogenetic Tree content type.

File

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

Code

function tripal_create_bundle($args, &$error = '') {
  $vocabulary = $args['vocabulary'];
  $accession = $args['accession'];
  $term_name = $args['term_name'];
  $storage_args = $args['storage_args'];

  $transaction = db_transaction();
  try {
    // First create the TripalVocab if it doesn't already exist.
    $vocab = tripal_load_vocab_entity(array('vocabulary' => $vocabulary));
    if (!$vocab) {
      $vocab = entity_get_controller('TripalVocab')->create(array('vocabulary' => $vocabulary));
      $vocab->save();
    }

    // Next create the TripalTerm if it doesn't already exist.
    $term = tripal_load_term_entity(array(
      'vocabulary' => $vocabulary,
      'accession' => $accession
    ));
    if (!$term) {
      $targs = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
      $term = entity_get_controller('TripalTerm')->create($targs);
      $term = $term->save();
    }

    // If the bundle doesn't already exist, then add it.
    $bundle_name = 'bio_data_' . $term->id;
    $einfo = entity_get_info('TripalEntity');
    if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
      // Make the label for the content type have capitalized words.  The
      // exception is 'mRNA' which we know should not be uppercased.
      $label = ucwords(preg_replace('/_/', ' ', $term_name));
      if ($term_name == 'mRNA') {
        $label = $term_name;
      }
      // Insert the bundle.
      db_insert('tripal_bundle')
        ->fields(array(
          'label' => $label,
          'type' => 'TripalEntity',
          'name' => $bundle_name,
          'term_id' => $term->id,
        ))
        ->execute();
    }

    $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
    $modules = module_implements('bundle_create');
    foreach ($modules as $module) {
      $function = $module . '_bundle_create';
      $function($bundle, $storage_args);
    }

    // Clear the entity cache so that Drupal will read our
    // hook_entity_info() implementation.
    global $language;
    $langcode = $language->language;
    cache_clear_all("entity_info:$langcode", 'cache');
    variable_set('menu_rebuild_needed', TRUE);

    // Get the bundle object.
    $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));

    tripal_create_bundle_fields($bundle, $term);

    $modules = module_implements('bundle_postcreate');
    foreach ($modules as $module) {
      $function = $module . '_bundle_postcreate';
      $function($bundle);
    }
  }
  catch (Exception $e) {
    $transaction->rollback();
    drupal_set_message(t("Failed to create content type: %message.", 
    array('%message' => $e->getMessage())), 'error');
    return FALSE;
  }

  return $bundle;
}