瀏覽代碼

Fixed bugs from chainging entity type to BioData

Stephen Ficklin 9 年之前
父節點
當前提交
8aa95796ec

+ 6 - 20
tripal_entities/includes/TripalBundleUIController.inc

@@ -10,19 +10,7 @@
 class TripalBundleUIController extends EntityDefaultUIController {
 
   public function __construct($entity_type, $entity_info) {
-    // The 'bundle of' property is usually set in the hook_entity_info()
-    // function for the "entity type" entity.  This allows the Entity API
-    // to provide the user interface for managing fields attached to the
-    // bundle.  But, we are using the same controller classes for
-    // all entity types and we do not want new links for every
-    // entity type (vocabulary) on the Administration >> Structure menu.
-    // We just want one menu item.  So to support one menu item that
-    // can handle all of the Tripal entity types, we have to set the
-    // 'bundle of' property here rather than in the hook_entity_info() function.
-//     $bundle_of = $entity_type;
-//     $bundle_of = preg_replace('/_bundle/', '', $bundle_of);
-//     $entity_info['bundle of'] = $bundle_of;
-//     parent::__construct($entity_type, $entity_info);
+     parent::__construct($entity_type, $entity_info);
   }
 
   /**
@@ -33,16 +21,14 @@ class TripalBundleUIController extends EntityDefaultUIController {
     $items[$this->path]['description'] = 'Manage Tripal data types, including adding
       and removing fields and the display of fields.';
 
-    // We don't want to let the user add new Tripal data types. They
-    // are added automatically.
-    unset($items[$this->path . '/add']);
+    // We don't want to let the user import new Tripal data types.
     unset($items[$this->path . '/import']);
 
-    $items[$this->path . '/publish'] = array(
-      'title' => 'Add new biological data',
-      'description' => 'Publish Data',
+    $items[$this->path . '/add'] = array(
+      'title' => 'Add new biological data type',
+      'description' => 'Add new biological data type',
       'page callback' => 'drupal_get_form',
-      'page arguments' => array('tripal_entities_admin_publish_form'),
+      'page arguments' => array('tripal_entities_admin_add_type_form'),
       'access arguments' => array('administer tripal data types'),
       'file' =>  'includes/tripal_entities.admin.inc',
       'file path' => drupal_get_path('module', 'tripal_entities'),

+ 28 - 35
tripal_entities/includes/TripalEntity.inc

@@ -10,48 +10,41 @@ class TripalEntity extends Entity {
     // also therefore have a chado_entity record.  We want to
     // load this record so it is always part of the entity object.
     if (property_exists($this, 'id') and $this->id) {
-      // For Tripal created entities add in the chado_entity details.
-      $dbs = tripal_entities_get_db_names_for_published_vocabularies();
-      if (in_array($this->type, $dbs)) {
-        $details = db_select('chado_entity', 'ce')
-          ->fields('ce')
-          ->condition('entity_id', $this->id)
-          ->execute()
-          ->fetchObject();
+      $details = db_select('chado_entity', 'ce')
+        ->fields('ce')
+        ->condition('entity_id', $this->id)
+        ->execute()
+        ->fetchObject();
 
-        // Add the chado entity details to the entity in case it's needed
-        // downstream (e.g. in field widget construction).
-        $this->chado_entity_id = $details->chado_entity_id;
-        $this->chado_table = $details->data_table;
-        $this->chado_field = $details->field;
+      // Add the chado entity details to the entity in case it's needed
+      // downstream (e.g. in field widget construction).
+      $this->chado_entity_id = $details->chado_entity_id;
+      $this->chado_table = $details->data_table;
+      $this->chado_field = $details->field;
 
-        // Add in the record.
-        $schema = chado_get_schema($this->chado_table);
-        $pkey = $schema['primary key'][0];
-        $this->chado_record_id = $details->record_id;
-        $this->chado_record = chado_generate_var($this->chado_table, array($pkey =>+ $details->record_id));
-      }
+      // Add in the record.
+      $schema = chado_get_schema($this->chado_table);
+      $pkey = $schema['primary key'][0];
+      $this->chado_record_id = $details->record_id;
+      $this->chado_record = chado_generate_var($this->chado_table, array($pkey =>+ $details->record_id));
     }
     // If we do not have an ID then we need to do a few queries to get
     // information about the chado table this entity maps to.
     else {
-//       // Use the cvterm_id to look up the base table for this term.  We find
-//       // the base table by looking in the term_usage table for the mapping.
-//       $sel_values = array(
-//         'term_id' => array(
-//           'cvterm_id' => $this->cvterm_id,
-//         ),
-//       );
-//       $term_usage = chado_generate_var('tripal_term_usage', $sel_values, array('return_array' => 1));
+      // Use the cvterm_id to look up the base table for this term.  We find
+      // the base table by looking in the term_usage table for the mapping.
+      $sel_values = array(
+        'term_id' => array(
+          'cvterm_id' => $this->cvterm_id,
+        ),
+      );
+      $term_usage = chado_generate_var('tripal_term_usage', $sel_values, array('return_array' => 1));
 
-//       // For each table that uses this term, insert the field recursively
-//       foreach ($term_usage as $usage) {
-//         $this->chado_table = $usage->data_table;
-//         $this->chado_field = $usage->field;
-//       }
-      $this->chado_table = '';
-      $this->chado_field = '';
-      $this->chado_entity_id = NULL;
+      // For each table that uses this term, insert the field recursively
+      foreach ($term_usage as $usage) {
+        $this->chado_table = $usage->data_table;
+        $this->chado_field = $usage->field;
+      }
     }
   }
 

+ 5 - 12
tripal_entities/includes/TripalEntityController.inc

@@ -35,20 +35,12 @@ class TripalEntityController extends EntityAPIController {
     // The incoming values should have at a minimum the bundle_id;
     $bundle = $values['bundle'];
     $matches = array();
-    if (preg_match('/(.*)_(.*)/', $bundle, $matches)) {
-      $type = $matches[1];
-      $accession = $matches[2];
-      $values['type'] = $type;
+    if (preg_match('/dbxref_(.*)/', $bundle, $matches)) {
+      $dbxref_id = $matches[1];
+      $values['type'] = 'BioData';
 
       // Get the CVterm.
-      $match = array(
-        'dbxref_id' => array(
-          'accession' => $accession,
-          'db_id' => array(
-            'name' => $type,
-          ),
-        ),
-      );
+      $match = array('dbxref_id' => $dbxref_id);
       $cvterm = chado_generate_var('cvterm', $match);
       if ($cvterm) {
         $values['cvterm_id'] = $cvterm->cvterm_id;
@@ -160,6 +152,7 @@ class TripalEntityController extends EntityAPIController {
       $transaction->rollback();
       watchdog_exception('tripal_core', $e);
       drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
+      return FALSE;
     }
   }
 }

+ 25 - 35
tripal_entities/includes/TripalEntityUIController.inc

@@ -26,34 +26,37 @@ class TripalEntityUIController extends EntityDefaultUIController {
       'type' => MENU_LOCAL_TASK,
     );
 
-    // Change the add page menu to multiple types of entities
-    $items[$this->path . '/add'] = array(
-      'title' => 'Add new biological data',
-      'description' => 'Add new biological data',
-      'page callback'  => 'drupal_get_form',
-      'page arguments' => array('tripal_entities_entity_form'),
-      'access callback'  => 'tripal_entities_entity_access',
-      'access arguments' => array('administer tripal data'),
+    $items['BioData/add'] = array(
+      'title' => 'Add Biological Data',
+      'page callback' => 'tripal_entities_add_page',
       'file' =>  'includes/tripal_entities.entity_form.inc',
       'file path' => drupal_get_path('module', 'tripal_entities'),
-      'type' => MENU_LOCAL_ACTION,
-      'weight' => 20,
-    );
-
-     // Set a custom page for adding new tripal data entities.
-    $items['BioData/add'] = array(
-      'title' => 'Add Tripal data',
-      'description' => 'Add a new tripal data record',
-      'page callback'  => 'drupal_get_form',
-      'page arguments' => array('tripal_entities_entity_form'),
       'access callback'  => 'tripal_entities_entity_access',
       'access arguments' => array('edit'),
-      'file' =>  'includes/tripal_entities.entity_form.inc',
-      'file path' => drupal_get_path('module', 'tripal_entities'),
-      'type' => MENU_NORMAL_ITEM,
-      'weight' => 20,
     );
 
+
+    // Add a menu item for creating each bundle
+    $bundles = array_keys($this->entityInfo['bundles']);
+    foreach ($bundles as $bundle) {
+      $matches = array();
+      preg_match('/^dbxref_(.*?)$/', $bundle, $matches);
+      $dbxref_id = $matches[1];
+      $cvterm = chado_generate_var('cvterm', array('dbxref_id' => $dbxref_id));
+
+      // Set a custom page for adding new tripal data entities.
+      $items['BioData/add/' . $dbxref_id] = array(
+        'title' => 'Add ' . ucfirst($cvterm->name),
+        'description' => 'A ' . $cvterm->name . ' record.',
+        'page callback'  => 'drupal_get_form',
+        'page arguments' => array('tripal_entities_entity_form', 2),
+        'access callback'  => 'tripal_entities_entity_access',
+        'access arguments' => array('edit'),
+        'file' =>  'includes/tripal_entities.entity_form.inc',
+        'file path' => drupal_get_path('module', 'tripal_entities'),
+      );
+    }
+
     // Link for viewing a tripal data type.
     $items['BioData/' . $wildcard] = array(
       'title callback' => 'tripal_entities_entity_title',
@@ -74,7 +77,6 @@ class TripalEntityUIController extends EntityDefaultUIController {
       'access arguments' => array('view', 1),
       'type' => MENU_DEFAULT_LOCAL_TASK,
       'weight' => -10,
-
     );
 
     // 'Edit' tab for an individual entity page.
@@ -90,18 +92,6 @@ class TripalEntityUIController extends EntityDefaultUIController {
       'weight' => -8,
     );
 
-    $items['BioData/' . $wildcard . '/manage'] = array(
-      'title' => 'Manage',
-      'page callback' => 'tripal_entity_manage_fields',
-      'page arguments' => array(1),
-      'access callback' => 'tripal_entities_entity_access',
-      'access arguments' => array('manage', 1),
-      'file' =>  'includes/TripalEntityUIController.inc',
-      'file path' => drupal_get_path('module', 'tripal_entities'),
-      'type' => MENU_LOCAL_TASK,
-      'weight' => -6,
-    );
-
     // Menu item for deleting tripal data entities.
     $items['BioData/' . $wildcard . '/delete'] = array(
       'title'  => 'Delete',

+ 235 - 157
tripal_entities/includes/tripal_entities.admin.inc

@@ -63,20 +63,6 @@ function tripal_entities_content_view() {
 
 }
 
-function tripal_entities_publish($term_name = '') {
-
-  $form = drupal_get_form('tripal_entities_entity_form', $term_name);
-  $output = drupal_render($form);
-
-  // set the breadcrumb
-  $breadcrumb = array();
-  $breadcrumb[] = l('Home', '<front>');
-  $breadcrumb[] = l('Administration', 'admin');
-  drupal_set_breadcrumb($breadcrumb);
-
-  return $output;
-}
-
 /**
  *
  * @param unknown $form
@@ -207,187 +193,279 @@ function tripal_entities_admin_bundles_form_submit($form, $form_state) {
  * @param unknown $form_state
  * @return multitype:
  */
-function tripal_entities_admin_publish_form($form, &$form_state) {
-  $form = array();
+function tripal_entities_admin_add_type_form($form, &$form_state) {
 
-  // Set the defaults.
-  $cv_id = NULL;
-  $term_name = NULL;
+  $term_name = '';
+  $num_terms = 0;
+  $cv_id = '';
 
   // Set defaults using the form state.
-  if (array_key_exists('values', $form_state)) {
-    $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
-    $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
+  if (array_key_exists('storage', $form_state) and
+      array_key_exists('terms', $form_state['storage'])) {
+    $terms = $form_state['storage']['terms'];
+    $num_terms = count($terms);
+  }
+  if (array_key_exists('values', $form_state) and
+      array_key_exists('term_name', $form_state['values'])) {
+    $term_name = $form_state['values']['term_name'];
   }
 
-  // Let the user select the vocabulary and tripal_entity but only if they haven't
-  // already selected a tripal_entity.
-//   $sql = "
-//     SELECT CV.cv_id, CV.name
-//     FROM {tripal_vocabulary} TET
-//       INNER JOIN {cv} CV on CV.cv_id = TET.cv_id
-//     ORDER BY CV.name
-//   ";
-
-//   $vocabs = chado_query($sql);
-//   $cvs = array();
-//   while ($vocab = $vocabs->fetchObject()) {
-//     $cvs[$vocab->cv_id] = $vocab->name;
-//   }
-  $cvs = tripal_get_cv_select_options();
-  $form['cv_id'] = array(
-    '#type' => 'select',
-    '#title' => t('Vocabulary'),
-    '#options' => $cvs,
-    '#required' => TRUE,
-    '#description' => t('Select a vocabulary that contains the term you would like to set as publishable. Only vocabularies that are linked to data are shown.'),
-    '#default_value' => $cv_id,
-    '#ajax' => array(
-      'callback' => "tripal_entities_admin_publish_form_ajax_callback",
-      'wrapper' => 'tripal_entities_admin_publish_form',
-      'effect' => 'fade',
-      'method' => 'replace'
-    )
-  );
-
-  // If we have a CV ID then we want to provide an autocomplete field
-  if ($cv_id) {
-    $form['cvterm_select']['term_name'] = array(
-      '#title'       => t('Data Type'),
+  // If no term has been selected yet then provide the auto complete field.
+  if ($num_terms == 0) {
+    $form['term_name'] = array(
+      '#title'       => t('Biological Data Type'),
       '#type'        => 'textfield',
-      '#description' => t("Please enter the name of the data type to set as publishable.  The data type must be a valid term in the selected vocabulary. This field will autopopulate as you type to help find available data types."),
+      '#description' => t("Please enter the type of data that you want to add.
+          Once added, priviledged users can add new records of the selected
+          type. As you type, suggestions will be provided."),
       '#required'    => TRUE,
       '#default_value' => $term_name,
       '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
     );
+  }
+
+
+  // If the term belongs to more than one vocabulary then add additional fields
+  // to let the user select the vocabulary.
+  if ($num_terms > 1) {
+    $cvs = array();
+    foreach ($terms as $term) {
+      $cvs[$term->cv_id->cv_id] = $term->cv_id->name;
+    }
+    $form['cv_id'] = array(
+      '#type' => 'radios',
+      '#title' => t('Select the appropriate vocabulary'),
+      '#options' => $cvs,
+      '#description' => t('The term belongs to more than one vocabulary. Please
+          indicate the proper vocabulary for the term.')
+    );
+  }
 
-    $form['cvterm_select']['select_button'] = array(
+  // Add in the button for the cases of no terms or too many.
+  if ($num_terms != 1) {
+    $form['select_button'] = array(
       '#type' => 'submit',
-      '#value' => t('Publish Data'),
-      '#name' => 'publish',
+      '#value' => t('Use this term'),
+      '#name' => 'select_cvterm'
     );
   }
 
-
-  $form['#prefix'] = '<div id="tripal_entities_admin_publish_form">';
-  $form['#suffix'] = '</div>';
   return $form;
 }
 
-/**
- * An Ajax callback for the tripal_entities_admin_publish_form..
- */
-function tripal_entities_admin_publish_form_ajax_callback($form, $form_state) {
-  // return the form so Drupal can update the content on the page
-  return $form;
-}
 /**
  * Implements hook_validate() for the tripal_entities_admin_publish_form.
  *
  */
-function tripal_entities_admin_publish_form_validate($form, &$form_state) {
-  $cv_id = $form_state['values']['cv_id'];
-  $term_name = $form_state['values']['term_name'];
-
-  // Make sure the term_name is a real term in the vocabulary.
-  $type = tripal_get_cvterm(array(
-    'name' => $term_name,
-    'cv_id' => $cv_id
-  ));
-  if (!$type) {
-    form_set_error('term_name', t("The data type is not a valid name for the selected vocabulary."));
-  }
-
-  // Make sure the term is used in the site:
-  $values = array(
-    'cvterm_id' => $type->cvterm_id,
-  );
-  $bundles = chado_select_record('tripal_term', array('term_id'), $values);
-  if (count($bundles) == 0) {
-    form_set_error('term_name', t("The data type, %type, is not associated with data on this site and thus cannot be set as publishable.", array('%type' => $term_name)));
-  }
-
-  // Make sure the term is not already published.
-  $values = array(
-    'cvterm_id' => $type->cvterm_id,
-    'publish' => 1,
-  );
-  $bundles = chado_select_record('tripal_term', array('term_id'), $values);
-  if (count($bundles) > 0) {
-    form_set_error('term_name', t("This data type is already set as publishable."));
+function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
+  // Check if this term and vocabulary is in the tripal_vocabulary usage tables.
+  // If not then add it.
+  if (array_key_exists('clicked_button', $form_state) and
+      $form_state['clicked_button']['#name'] =='select_cvterm') {
+
+    // First, make sure the term is unique. If not then we can't check it.
+    $term_name = $form_state['values']['term_name'];
+    $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
+    $cvterm = NULL;
+
+    // If a term and cv_id are provided then we can look for the term using
+    // both and we should find a unique term. If only ther term is provided
+    // we can still look for a unique term but there must only be one.
+    if ($term_name and !$cv_id) {
+      $match = array(
+        'name' => $term_name,
+      );
+    }
+    else {
+      $match = array(
+        'name' => $term_name,
+        'cv_id' => $cv_id,
+      );
+    }
+    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
+
+    // Add the cvterm to the storage element so we don't have to keep
+    // looking it up in the submit function or on a rebuild of the form.
+    $form_state['storage']['terms'] = $terms;
+
+    // If we only have one term then we found a unique match and we can do
+    // some further checking.
+    if (count($terms) == 1) {
+      $cvterm = $terms[0];
+      // Make sure the term is set as published.
+      tripal_entities_add_term_usage($cvterm, $form_state);
+    }
+    // If we do not have any terms then the term provided by the user does not
+    // exists and we need to provide an error message.
+    if (count($terms) == 0) {
+      form_set_error('term_name', t('The term does not exist in this database.'));
+    }
+    // If we have more than one term then we need to set an error so that the
+    // form can provide a list of vocabularies to select from.
+    if (count($terms) > 1) {
+      form_set_error('', t('The term is not unique. A list of vocabularies has been provided where this term is present. Please select the appropriate one.'));
+    }
   }
 }
 /**
  * Implements hook_submit() for the tripal_entities_admin_publish_form.
  *
  */
-function tripal_entities_admin_publish_form_submit($form, &$form_state) {
-  $cv_id = $form_state['values']['cv_id'];
-  $term_name = $form_state['values']['term_name'];
-
-  // Get the data type using the $term_name and $cv_id.
-  $cvterm = chado_generate_var('cvterm', array('cv_id' => $cv_id, 'name' => $term_name));
-
-  // Start the transaction.
-  $transaction = db_transaction();
-  try {
-
-    // We don't need to check if the vocabulary is used because the
-    // form only shows those vocabs that are used.
+function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
+  if ($form_state['clicked_button']['#name'] =='select_cvterm') {
+    $cvterm = $form_state['storage']['terms'][0];
+    $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
+
+    // Before we try to add this type, check to see if it already exists
+    // as a bundle.
+    $einfo = entity_get_info('BioData');
+    if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
+      tripal_entities_add_term_usage($cvterm, $form_state);
+      tripal_entities_add_bundle($cvterm);
+      drupal_set_message('New biological data type created.  Fields are added automatically to this type.');
+      $form_state['redirect'] = "admin/structure/BioData";
+    }
+    else {
+      drupal_set_message('This type already exists.', 'warning');
+    }
+  }
+}
 
-    // Mark this entity as published.
-    $match = array('cv_id' => $cv_id);
+/**
+ * Adds a vocabulary and term to the Tripal term usage tables.
+ *
+ * This function is meant to be called only by the
+ * tripal_entities_entity_form_validate() function. This code is
+ * separated to simplify that function.  Therefore, if errors occur with adding
+ * of terms then the form_set_error() is called.
+ *
+ * @param $cvterm
+ */
+function tripal_entities_add_term_usage($cvterm, &$form_state) {
+
+  // Before creating the entity we mut add records to the tripal_vocabulary
+  // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
+  $match = array('cv_id' => $cvterm->cv_id->cv_id);
+  $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
+  if (count($vocab) == 0) {
+    $values = array(
+      'cv_id' => $cvterm->cv_id->cv_id,
+      'db_id' => $cvterm->dbxref_id->db_id->db_id,
+      'publish' => 1,
+    );
+    $values = chado_insert_record('tripal_vocabulary', $values);
+    if (!$values) {
+      form_set_error('', 'Could not add vocabulary to tripal_vocabluary table.');
+      return FALSE;
+    }
+    // Convert the values array into an object.
+    $vocab = new stdClass();
+    $vocab->vocabulary_id = $values['vocabulary_id'];
+    $vocab->cv_id = $values['cv_id'];
+  }
+  else {
+    // Make sure the vocabulary is set to publish
     $values = array('publish' => 1);
-    $success = chado_update_record('tripal_vocabulary', $match, $values);
-    if (!$success) {
-      throw new Exception('Cannot set the vocabulary as publishable');
+    chado_update_record('tripal_vocabulary', $match, $values);
+    $vocab = $vocab[0];
+  }
+
+  // Does this vocabulary have a record in the tripal_vocabulary_usage
+  // table? If not then add one.
+  $match = array('vocabulary_id' => $vocab->vocabulary_id);
+  $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
+  if (count($vocab_usage) == 0) {
+    // Look to see if this vocabulary is used as a default for any table. If
+    // so then we can use that to populate the tripal_vocabulary_usage table.
+    $default = db_select('tripal_cv_defaults', 't')
+    ->fields('t')
+    ->condition('cv_id', $vocab->cv_id)
+    ->execute()
+    ->fetchObject();
+    if ($default) {
+      $values = array(
+        'vocabulary_id' => $vocab->vocabulary_id,
+        'data_table' => $default->table_name,
+        'type_table' => $default->table_name,
+        'field' =>  $default->field_name,
+      );
+      $values = chado_insert_record('tripal_vocabulary_usage', $values);
+      if (!$values) {
+        form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
+        return FALSE;
+      }
     }
+    // If there is no default table then we have an error, and we should
+    // set a variable so that the form can help the user deal with the problem.
+    else {
+      $form_state['storage']['cvterm_has_default'] = FALSE;
+      form_set_error('', t('There is no default mapping of this term\'s
+          vocabulary to a table in Chado.  Therefore, it is not possible to
+          determine how to store data of this type.'));
+      return FALSE;
+    }
+    $vocab_usage = new stdClass();
+    $vocab_usage->vocabulary_id = $values['vocabulary_id'];
+    $vocab_usage->data_table = $values['data_table'];
+    $vocab_usage->type_table = $values['type_table'];
+    $vocab_usage->field = $values['field'];
+  }
+  else {
+    $vocab_usage = $vocab_usage[0];
+  }
 
-    // We have already checked in the validate if the term already exists
-    // as a bundle. So, if we're here we can enable it.
-    $match = array('cvterm_id' => $cvterm->cvterm_id);
-    $values = array('publish' => 1);
-    $success = chado_update_record('tripal_term', $match, $values);
-    if (!$success) {
-      throw new Exception('Cannot set the data type as publishable');
+  // Now add the tripal_term record if it doesn't already exist.
+  $match = array(
+    'vocabulary_id' => $vocab->vocabulary_id,
+    'cvterm_id' => $cvterm->cvterm_id,
+  );
+  $term = chado_select_record('tripal_term', array('*'), $match);
+  if (count($term) == 0) {
+    $values = array(
+      'vocabulary_id' => $vocab->vocabulary_id,
+      'cvterm_id' => $cvterm->cvterm_id,
+    );
+    $values = chado_insert_record('tripal_term', $values);
+    if (!$values) {
+      form_set_error('', 'Could not add term to tripal_term table..');
+      return FALSE;
     }
+    $term = new stdClass();
+    $term->term_id = $values['term_id'];
+  }
+  else {
+    $values = array('publish' => 1);
+    chado_update_record('tripal_term', $match, $values);
+    $term = $term[0];
+  }
 
-    // Create the bundle name and entity type name.
-    $bundle_name = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
-    $entity_type_name = 'BioData'; // $cvterm->dbxref_id->db_id->name;
-
-    // Clear the entity cache so that Drupal will read our
-    // hook_entity_info() implementation which now will have the entities
-    // described because we set the publish column to 1 in the tripal_term
-    // table.
-    global $language;
-    $langcode = $language->language;
-    cache_clear_all("entity_info:$langcode", 'cache');
-
-    // The TripalBundle Entity manages the bundles we have available.
-    // Therefore, we need to add a new entity for each bundle "type".
-    $vals = array(
-      'label' => $bundle_name  . ' (' . $cvterm->name . ')',
-      'type' => $entity_type_name,
-      'bundle' => $bundle_name,
-      'data' => serialize(array()),
-      'module' => 'tripal_entities'
+  // Finally, add the tripal_term_usage record if it doesn't already exist.
+  $match = array('term_id' => $term->term_id);
+  $options = array('has_record' => TRUE);
+  if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
+    $values = array(
+      'term_id' => $term->term_id,
+      'data_table' => $vocab_usage->data_table,
+      'type_table' => $vocab_usage->type_table,
+      'field' =>  $vocab_usage->field,
     );
-    $tripal_bundle = new TripalBundle($vals, $entity_type_name . '_bundle');
-    $tripal_bundle->save();
+    $values = chado_insert_record('tripal_term_usage', $values);
+    if (!$values) {
+      form_set_error('', 'Could not add term to tripal_term table..');
+      return FALSE;
+    }
+  }
 
-    // Allow modules to now add fields to the bundle
-    module_invoke_all('add_bundle_fields', $entity_type_name, $bundle_name, $cvterm);
+  // Clear the entity cache so that Drupal will read our
+  // hook_entity_info() implementation which now will have the entities
+  // described because we set the publish column to 1 in the tripal_term
+  // table.
+  global $language;
+  $langcode = $language->language;
+  cache_clear_all("entity_info:$langcode", 'cache');
 
-    drupal_set_message(t('Data type, %type, is now set as publishable.', array('%type' => $term_name)));
-  }
-  catch (Exception $e) {
-    $transaction->rollback();
-    drupal_set_message('Failure publishing this data type: ' . $e->getMessage(), 'error');
-    watchdog_exception('trp_entities', $e);
-  }
+  return TRUE;
 }
-
 /**
  * Implements hook_chado_field_alter.
  *

+ 56 - 366
tripal_entities/includes/tripal_entities.entity_form.inc

@@ -3,171 +3,53 @@
 /**
  *
  */
-function tripal_entities_entity_form($form, &$form_state, $term_name = '', $entity = NULL) {
-  // Set the defaults.
-  $cv_id = NULL;
-  $cvterm = NULL;
-  $do_sync = 0;
-  $terms = array();
-  $num_terms = 0;
-
-  // Set defaults if an entity was provided.
-  if ($entity) {
-    drupal_set_title('Edit ' . $entity->title);
-    $id = $entity->id;
-    $values = array('cvterm_id' => $entity->cvterm_id);
-    $cvterm = chado_generate_var('cvterm', $values);
-    $cv_id = $cvterm->cv_id->cv_id;
-    $term_name = $cvterm->name;
-    $terms[]  = $cvterm;
-    $num_terms = 1;
-  }
-
-  // Set defaults using the form state.
-  if (array_key_exists('storage', $form_state) and
-      array_key_exists('terms', $form_state['storage'])) {
-    $terms = $form_state['storage']['terms'];
-    $num_terms = count($terms);
-  }
-  // If we have a term name but the $form_state['storage']['terms'] isn't set
-  // then the term_name is being passed in, and we should try to get the
-  // terms.
-  else if ($term_name) {
-    $match = array(
-      'name' => $term_name,
-    );
-    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
-    $form_state['storage']['terms'] = $terms;
-    $num_terms = count($terms);
+function tripal_entities_add_page() {
+  $item = menu_get_item();
 
-    // Make sure the term is set as published.
-    if ($num_terms == 1) {
-      tripal_entities_add_term_usage($terms[0], $form_state);
-    }
-    $einfo = entity_get_info($terms[0]->dbxref_id->db_id->name);
-    tripal_entities_add_bundle($terms[0]);
-  }
-  if (array_key_exists('values', $form_state) and
-      array_key_exists('term_name', $form_state['values'])) {
-    $term_name = $form_state['values']['term_name'];
-  }
-  if (array_key_exists('values', $form_state) and
-      array_key_exists('do_sync', $form_state['values'])) {
-    $do_sync = $form_state['values']['do_sync'];
-  }
+  $content = system_admin_menu_block($item);
 
-  // If no term has been selected yet then provide the auto complete field.
-  if ($num_terms != 1) {
-    $cvterms = tripal_entities_get_published_terms_as_select_options($cv_id);
-    $form['term_name'] = array(
-      '#title'       => t('Data Type'),
-      '#type'        => 'textfield',
-      '#description' => t("Please enter the type of data that you want to publish.  As you type, suggestions will be provided."),
-      '#required'    => TRUE,
-      '#default_value' => $term_name,
-      '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
-    );
-    // If we are not editing an existing entity then provide a radio button
-    // to allow the user to add a new record or to sync existing records.
-    if (!$entity) {
-      $action = array(0 => t('Publish a new record'), 1 => t('Publish one or more existing records'));
-      $form['do_sync'] = array(
-        '#type' => 'radios',
-        '#title' => t('Publish Action'),
-        '#default_value' => $do_sync,
-        '#options' => $action,
-      );
-    }
+  // Bypass the node/add listing if only one content type is available.
+  if (count($content) == 1) {
+    $item = array_shift($content);
+    drupal_goto($item['href']);
   }
+  return theme('tripal_entities_add_list', array('content' => $content));
 
+}
 
-  // If the term belongs to more than one vocabulary then add additional fields
-  // to let the user select the vocabulary.
-  if ($num_terms > 1) {
-    $cvs = array();
-    foreach ($terms as $term) {
-      $cvs[$term->cv_id->cv_id] = $term->cv_id->name;
+/**
+ * Returns HTML for a list of available node types for node creation.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - content: An array of content types.
+ *
+ * @ingroup themeable
+ */
+function theme_tripal_entities_add_list($variables) {
+  $content = $variables['content'];
+  $output = '';
+
+  if ($content) {
+    $output = '<dl class="node-type-list">';
+    foreach ($content as $item) {
+      $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
+      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
     }
-    $form['cv)id'] = array(
-      '#type' => 'radios',
-      '#title' => t('Select the appropriate vocabulary'),
-      '#options' => $cvs,
-      '#description' => t('The term belongs to more than one vocabulary. Please
-          indicate the proper vocabulary for the term.')
-    );
-  }
-
-  // Add in the button for the cases of no terms or too many.
-  if ($num_terms != 1) {
-    $form['select_button'] = array(
-      '#type' => 'submit',
-      '#value' => t('Use this term'),
-      '#name' => 'select_cvterm'
-    );
+    $output .= '</dl>';
   }
-
-  // If we only have one cvterm then the user has provided a unique
-  // term and we can either allow them to add a new entry or sync existing
-  // records.
-  if ($num_terms == 1){
-    $cvterm = $terms[0];
-    //$bundle_id = $cvterm->dbxref_id->db_id->name .'_' .$cvterm->dbxref_id->accession;
-    $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
-
-    $form['cv_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $cvterm->cv_id->cv_id
-    );
-    $form['type'] = array(
-      '#type' => 'hidden',
-      '#value' => $cvterm->dbxref_id->db_id->name
-    );
-    $form['term_name'] = array(
-      '#type' => 'hidden',
-      '#value' => $cvterm->name
-    );
-    $form['cvterm_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $cvterm->cvterm_id
-    );
-    $form['bundle'] = array(
-      '#type' => 'hidden',
-      '#value' => $bundle_id
-    );
-    $form['details'] = array(
-      '#type' => 'item',
-      '#title' => 'Record Type',
-      '#markup' => '(' . $cvterm->cv_id->name . ') ' . $cvterm->name,
-      '#weight' => -1000,
-    );
-
-    if(!$do_sync) {
-      tripal_entities_entity_form_add_new($bundle_id, $cvterm, $form, $form_state, $entity);
-    }
-    else {
-      tripal_entities_entity_form_add_sync($bundle_id, $cvterm, $form, $form_state, $entity);
-    }
-
-    // Add a prefix and a suffix around this form so that fields may
-    // replace the entire form on an ajax call if they want.
-    $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
-    $form['#suffix'] = "</div>";
+  else {
+    $output = '<p>' . t('You have not created any biological types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/BioData/add'))) . '</p>';
   }
-  return $form;
-}
-/**
- * Adds fields to the tripal_entities_entity_form for syncing an entity.
- */
-function tripal_entities_entity_form_add_sync($bundle_id, $cvterm, &$form, &$form_state, $entity) {
-  $form['todo'] = array(
-    '#type' => 'item',
-    '#markup' => t('TODO: Add a form for allowing the user to filter by fields'),
-  );
+  return $output;
 }
+
 /**
- * Adds fields to the tripal_entities_entity_form for editing/adding an entity.
+ *
  */
-function tripal_entities_entity_form_add_new($bundle_id, $cvterm, &$form, &$form_state, $entity) {
+function tripal_entities_entity_form($form, &$form_state, $dbxref_id = '', $entity = NULL) {
+
+  $bundle_id = 'dbxref_' . $dbxref_id;
 
   // Add a vertical tabs element
   $form['ev_tabs'] = array(
@@ -189,10 +71,6 @@ function tripal_entities_entity_form_add_new($bundle_id, $cvterm, &$form, &$form
   }
   else {
     field_attach_form('BioData', $entity, $form, $form_state);
-    $form['entity_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $entity->id
-    );
     $form['update_button'] = array(
       '#type' => 'submit',
       '#value' => t('Update'),
@@ -209,7 +87,12 @@ function tripal_entities_entity_form_add_new($bundle_id, $cvterm, &$form, &$form
 
   // The entity object must be added to the $form_state in order for
   // the Entity API to work. It must have a key of the entity name.
-  $form_state[$cvterm->dbxref_id->db_id->name] = $entity;
+  $form_state['BioData'] = $entity;
+
+  $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
+  $form['#suffix'] = "</div>";
+
+  return $form;
 
 }
 /**
@@ -225,230 +108,37 @@ function tripal_entities_entity_form_ajax_callback($form, $form_state) {
  */
 function tripal_entities_entity_form_validate($form, &$form_state) {
 
-  // Check if this term and vocabulary is in the tripal_vocabulary usage tables.
-  // If not then add it.
-  if (array_key_exists('clicked_button', $form_state) and
-      $form_state['clicked_button']['#name'] =='select_cvterm') {
-
-    // First, make sure the term is unique. If not then we can't check it.
-    $term_name = $form_state['values']['term_name'];
-    $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
-    $cvterm = NULL;
-
-    // If a term and cv_id are provided then we can look for the term using
-    // both and we should find a unique term. If only ther term is provided
-    // we can still look for a unique term but there must only be one.
-    if ($term_name and !$cv_id) {
-      $match = array(
-        'name' => $term_name,
-      );
-    }
-    else {
-      $match = array(
-        'name' => $term_name,
-        'cv_id' => $cv_id,
-      );
-    }
-    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
-
-    // Add the cvterm to the storage element so we don't have to keep
-    // looking it up in the submit function or on a rebuild of the form.
-    $form_state['storage']['terms'] = $terms;
-
-    // If we only have one term then we found a unique match and we can do
-    // some further checking.
-    if (count($terms) == 1) {
-      $cvterm = $terms[0];
-      // Make sure the term is set as published.
-      tripal_entities_add_term_usage($cvterm, $form_state);
-    }
-    // If we do not have any terms then the term provided by the user does not
-    // exists and we need to provide an error message.
-    if (count($terms) == 0) {
-      form_set_error('term_name', t('The term does not exist in this database.'));
-    }
-    // If we have more than one term then we need to set an error so that the
-    // form can provide a list of vocabularies to select from.
-    if (count($terms) > 1) {
-      form_set_error('', t('The term is not unique. A list of vocabularies has been provided where this term is present. Please select the appropriate one.'));
-    }
-  }
-
-
   if (array_key_exists('clicked_button', $form_state) and
       $form_state['clicked_button']['#name'] =='add_data') {
-    $tripal_entity = (object) $form_state['values'];
-    $entity_type = $form_state['values']['type'];
-    field_attach_form_validate('BioData', $tripal_entity, $form, $form_state);
+    $entity = $form_state['BioData'];
+    field_attach_form_validate('BioData', $entity, $form, $form_state);
   }
 }
 
-/**
- * Adds a vocabulary and term to the Tripal term usage tables.
- *
- * This function is meant to be called only by the
- * tripal_entities_entity_form_validate() function. This code is
- * separated to simplify that function.  Therefore, if errors occur with adding
- * of terms then the form_set_error() is called.
- *
- * @param $cvterm
- */
-function tripal_entities_add_term_usage($cvterm, &$form_state) {
-
-  // Before creating the entity we mut add records to the tripal_vocabulary
-  // tripal_vocabulary_usage, tripal_term, and tripal_term_usage tables.
-  $match = array('cv_id' => $cvterm->cv_id->cv_id);
-  $vocab = chado_select_record('tripal_vocabulary', array('*'), $match);
-  if (count($vocab) == 0) {
-    $values = array(
-      'cv_id' => $cvterm->cv_id->cv_id,
-      'db_id' => $cvterm->dbxref_id->db_id->db_id,
-      'publish' => 1,
-    );
-    $values = chado_insert_record('tripal_vocabulary', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add vocabulary to tripal_vocabluary table.');
-      return FALSE;
-    }
-    // Convert the values array into an object.
-    $vocab = new stdClass();
-    $vocab->vocabulary_id = $values['vocabulary_id'];
-    $vocab->cv_id = $values['cv_id'];
-  }
-  else {
-    // Make sure the vocabulary is set to publish
-    $values = array('publish' => 1);
-    chado_update_record('tripal_vocabulary', $match, $values);
-    $vocab = $vocab[0];
-  }
-
-  // Does this vocabulary have a record in the tripal_vocabulary_usage
-  // table? If not then add one.
-  $match = array('vocabulary_id' => $vocab->vocabulary_id);
-  $vocab_usage = chado_select_record('tripal_vocabulary_usage', array('*'), $match);
-  if (count($vocab_usage) == 0) {
-    // Look to see if this vocabulary is used as a default for any table. If
-    // so then we can use that to populate the tripal_vocabulary_usage table.
-    $default = db_select('tripal_cv_defaults', 't')
-      ->fields('t')
-      ->condition('cv_id', $vocab->cv_id)
-      ->execute()
-      ->fetchObject();
-    if ($default) {
-       $values = array(
-        'vocabulary_id' => $vocab->vocabulary_id,
-        'data_table' => $default->table_name,
-        'type_table' => $default->table_name,
-        'field' =>  $default->field_name,
-      );
-      $values = chado_insert_record('tripal_vocabulary_usage', $values);
-      if (!$values) {
-        form_set_error('', 'Could not add vocabulary to tripal_vocabulary_usage table.');
-        return FALSE;
-      }
-    }
-    // If there is no default table then we have an error, and we should
-    // set a variable so that the form can help the user deal with the problem.
-    else {
-      $form_state['storage']['cvterm_has_default'] = FALSE;
-      form_set_error('', t('There is no default mapping of this term\'s
-          vocabulary to a table in Chado.  Therefore, it is not possible to
-          determine how to store data of this type.'));
-      return FALSE;
-    }
-    $vocab_usage = new stdClass();
-    $vocab_usage->vocabulary_id = $values['vocabulary_id'];
-    $vocab_usage->data_table = $values['data_table'];
-    $vocab_usage->type_table = $values['type_table'];
-    $vocab_usage->field = $values['field'];
-  }
-  else {
-    $vocab_usage = $vocab_usage[0];
-  }
-
-  // Now add the tripal_term record if it doesn't already exist.
-  $match = array(
-    'vocabulary_id' => $vocab->vocabulary_id,
-    'cvterm_id' => $cvterm->cvterm_id,
-  );
-  $term = chado_select_record('tripal_term', array('*'), $match);
-  if (count($term) == 0) {
-    $values = array(
-      'vocabulary_id' => $vocab->vocabulary_id,
-      'cvterm_id' => $cvterm->cvterm_id,
-    );
-    $values = chado_insert_record('tripal_term', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add term to tripal_term table..');
-      return FALSE;
-    }
-    $term = new stdClass();
-    $term->term_id = $values['term_id'];
-  }
-  else {
-    $values = array('publish' => 1);
-    chado_update_record('tripal_term', $match, $values);
-    $term = $term[0];
-  }
-
-  // Finally, add the tripal_term_usage record if it doesn't already exist.
-  $match = array('term_id' => $term->term_id);
-  $options = array('has_record' => TRUE);
-  if (!chado_select_record('tripal_term_usage', array('*'), $match, $options)) {
-    $values = array(
-      'term_id' => $term->term_id,
-      'data_table' => $vocab_usage->data_table,
-      'type_table' => $vocab_usage->type_table,
-      'field' =>  $vocab_usage->field,
-    );
-    $values = chado_insert_record('tripal_term_usage', $values);
-    if (!$values) {
-      form_set_error('', 'Could not add term to tripal_term table..');
-      return FALSE;
-    }
-  }
-
-  // Clear the entity cache so that Drupal will read our
-  // hook_entity_info() implementation which now will have the entities
-  // described because we set the publish column to 1 in the tripal_term
-  // table.
-  global $language;
-  $langcode = $language->language;
-  cache_clear_all("entity_info:$langcode", 'cache');
 
-  return TRUE;
-}
 /**
  * Implements hook_submit() for the tripal_entities_entity_form.
  */
 function tripal_entities_entity_form_submit($form, &$form_state) {
+  $entity = $form_state['BioData'];
+
   if ($form_state['clicked_button']['#name'] =='cancel') {
-    if (array_key_exists('id', $form_state['values'])) {
-      $entity_id = $form_state['values']['entity_id'];
-      $form_state['redirect'] = "BioData/$entity_id";
-    }
-    else {
-      $form_state['redirect'] = "admin/structure/tripal_entity";
-    }
-    return;
+    $form_state['redirect'] = "BioData/" . $entity->id;
   }
-  if ($form_state['clicked_button']['#name'] =='select_cvterm') {
-    // Check to see if the entity already exists. If not then create it.
-    $cvterm = $form_state['storage']['terms'][0];
-    $einfo = entity_get_info($cvterm->dbxref_id->db_id->name);
-    tripal_entities_add_bundle($cvterm);
 
-    $form_state['rebuild'] = TRUE;
-  }
   if ($form_state['clicked_button']['#name'] =='update_data' or
       $form_state['clicked_button']['#name'] =='add_data') {
-    $entity = entity_ui_controller('BioData')->entityFormSubmitBuildEntity($form, $form_state);
-    $entity->save();
-    $form_state['redirect'] = "BioData/$entity->id";
+
+    $entityform = entity_ui_controller('BioData')->entityFormSubmitBuildEntity($form, $form_state);
+    if ($entityform->save()) {
+      $form_state['redirect'] = "BioData/" . $entity->id;
+    }
+    else {
+      drupal_set_message('Cannot save entity', 'error');
+    }
   }
   if ($form_state['clicked_button']['#name'] =='delete_data') {
-    $entity_id = $form_state['values']['entity_id'];
-    $form_state['redirect'] = 'BioData/' .$entity_id .'/delete';
+    $form_state['redirect'] = 'BioData/' . $entity->id .'/delete';
   }
 }
 

+ 1 - 0
tripal_entities/tripal_entities.install

@@ -10,6 +10,7 @@ function tripal_entities_install() {
   // chado schema
   tripal_entities_add_custom_tables();
 
+
 }
 
 /**

+ 115 - 129
tripal_entities/tripal_entities.module

@@ -28,7 +28,7 @@ function tripal_entities_menu() {
   $items = array();
 
   // The administative settings menu.
-  $items['admin/tripal/bio_data'] = array(
+  $items['admin/tripal/BioData'] = array(
     'title' => 'Biological Data',
     'description' => 'Tools for publishing, configurating and managing biological data.',
     'page callback' => 'tripal_entities_admin_view',
@@ -44,25 +44,6 @@ function tripal_entities_menu() {
     'weight' =>  1,
   );
 
-  $items['admin/tripal/BioData/publish'] = array(
-    'title' => 'Publish',
-    'description' => 'Publish Data',
-    'page callback' => 'tripal_entities_publish',
-    'access arguments' => array('administer tripal data types'),
-    'file' =>  'includes/tripal_entities.admin.inc',
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 2
-  );
-  $items['admin/tripal/BioData/publish/%'] = array(
-    'title' => 'Publish',
-    'description' => 'Publish Data',
-    'page callback' => 'tripal_entities_publish',
-    'page arguments' => array(4),
-    'access arguments' => array('administer tripal data types'),
-    'file' =>  'includes/tripal_entities.admin.inc',
-    'type' => MENU_CALLBACK,
-  );
-
   $items['admin/tripal/BioData/access'] = array(
     'title' => 'Access',
     'description' => 'Set default access permissions for collections of data.',
@@ -77,6 +58,22 @@ function tripal_entities_menu() {
   return $items;
 }
 
+/**
+ * Implements hook_admin_paths().
+ */
+function tripal_entities_admin_paths() {
+  if (variable_get('node_admin_theme')) {
+    $paths = array(
+      'BioData/*/edit' => TRUE,
+      'BioData/*/delete' => TRUE,
+      'BioData/add' => TRUE,
+      'BioData/add/*' => TRUE,
+    );
+    return $paths;
+  }
+}
+
+
 /**
  * Implements hook_permission().
  */
@@ -110,6 +107,11 @@ function tripal_entities_theme($existing, $type, $theme, $path) {
       'render element' => 'element',
       'file' => 'includes/tripal_entities.chado_entity.inc',
     ),
+    'tripal_entities_add_list' => array(
+      'variables' => array('content' => NULL),
+      'file' => 'includes/tripal_entities.entity_form.inc',
+    ),
+
   );
 }
 
@@ -151,133 +153,117 @@ function tripal_entities_rdf_mapping() {
  */
 function tripal_entities_entity_info() {
   $entities = array();
+  $entities['BioData'] = array (
+    // A human readable label to identify our entity.
+    'label' => 'Biological Data',
+    'plural label' => 'Biological Data',
+
+    // The entity class and controller class extend the classes provided by the
+    // Entity API.
+    'entity class' => 'TripalEntity',
+    'controller class' => 'TripalEntityController',
+
+    // The table for this entity defined in hook_schema()
+    'base table' => 'tripal_entity',
+
+    // Returns the uri elements of an entity.
+    'uri callback' => 'tripal_entities_vocbulary_term_uri',
+
+    // IF fieldable == FALSE, we can't attach fields.
+    'fieldable' => TRUE,
+
+    // entity_keys tells the controller what database fields are used for key
+    // functions. It is not required if we don't have bundles or revisions.
+    // Here we do not support a revision, so that entity key is omitted.
+    'entity keys' => array (
+      'id' => 'id',
+      'bundle' => 'bundle'
+    ),
+    'bundle keys' => array (
+      'bundle' => 'bundle'
+    ),
 
-  // Get a list of published vocabularies from 'tripal_vocabulary
-//   $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
-
-//   foreach ($published_vocs as $voc) {
-    $entities['BioData'] = array (
-      // A human readable label to identify our entity.
-      'label' => 'Biological Data',
-      'plural label' => 'Biological Data',
-
-      // The entity class and controller class extend the classes provided by the
-      // Entity API.
-      'entity class' => 'TripalEntity',
-      'controller class' => 'TripalEntityController',
-
-      // The table for this entity defined in hook_schema()
-      'base table' => 'tripal_entity',
-
-      // Returns the uri elements of an entity.
-      'uri callback' => 'tripal_entities_vocbulary_term_uri',
-
-      // IF fieldable == FALSE, we can't attach fields.
-      'fieldable' => TRUE,
-
-      // entity_keys tells the controller what database fields are used for key
-      // functions. It is not required if we don't have bundles or revisions.
-      // Here we do not support a revision, so that entity key is omitted.
-      'entity keys' => array (
-        'id' => 'id',
-        'bundle' => 'bundle'
-      ),
-      'bundle keys' => array (
-        'bundle' => 'bundle'
-      ),
-
-      // Callback function for access to this entity.
-      'access callback' => 'tripal_entity_access',
+    // Callback function for access to this entity.
+    'access callback' => 'tripal_entity_access',
 
-      // FALSE disables caching. Caching functionality is handled by Drupal core.
-      'static cache' => FALSE,
+    // FALSE disables caching. Caching functionality is handled by Drupal core.
+    'static cache' => FALSE,
 
-      // Bundles are added in the hook_entities_info_alter() function.
-      'bundles' => array (),
+    // Bundles are added dynamically below.
+    'bundles' => array (),
 
-      'label callback' => 'tripal_entity_label',
+    'label callback' => 'tripal_entity_label',
 
-      // The information below is used by the TripalEntityUIController
-      // (which extends the EntityDefaultUIController). The admin_ui
-      // key here is mean to appear on the 'Find Content' page of the
-      // administrative menu.
-      'admin ui' => array (
-        'path' => 'admin/content/BioData',
-        'controller class' => 'TripalEntityUIController',
-        'menu wildcard' => '%tripal_entity',
-        'file' => 'includes/TripalEntityUIController.inc'
-      ),
-      'view modes' => array (
-        'full' => array (
-          'label' => t ( 'Full content' ),
-          'custom settings' => FALSE
-        ),
-        'teaser' => array (
-          'label' => t ( 'Teaser' ),
-          'custom settings' => TRUE
-        )
-      )
-    );
-    // The entity that holds information about the entity types.
-    $entities ['BioData_bundles'] = array (
-      'label' => 'Biological Data Type', //$voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
-      'entity class' => 'TripalBundle',
-      'controller class' => 'TripalBundleController',
-      'base table' => 'tripal_bundle',
-      'fieldable' => FALSE,
-      'exportable' => FALSE,
-      'entity keys' => array (
-        'id' => 'id',
-        'name' => 'bundle',
-        'label' => 'label'
+    // The information below is used by the TripalEntityUIController
+    // (which extends the EntityDefaultUIController). The admin_ui
+    // key here is mean to appear on the 'Find Content' page of the
+    // administrative menu.
+    'admin ui' => array (
+      'path' => 'admin/content/BioData',
+      'controller class' => 'TripalEntityUIController',
+      'menu wildcard' => '%tripal_entity',
+      'file' => 'includes/TripalEntityUIController.inc'
+    ),
+    'view modes' => array (
+      'full' => array (
+        'label' => t ('Full content'),
+        'custom settings' => FALSE
       ),
-      'access callback' => 'tripal_bundle_access',
-      'module' => 'tripal_entities',
-      // Enable the entity API's admin UI.
-      'admin ui' => array (
-        'path' => 'admin/structure/BioData', // . $voc->db_id->name,
-        'controller class' => 'TripalBundleUIController',
-        'file' => 'includes/TripalBundleUIController.inc',
-        'menu wildcard' => '%tripal_bundle',
+      'teaser' => array (
+        'label' => t ('Teaser'),
+        'custom settings' => TRUE
       )
-    );
-//   }
-  return $entities;
-}
-/**
- * Implements hook_entity_info_alter().
- *
- * We are adding the info about the tripal data types via a hook to avoid a
- * recursion issue as loading the model types requires the entity info as well.
- *
- */
-function tripal_entities_entity_info_alter(&$entity_info) {
+    )
+  );
+
+  // The entity that holds information about the entity types.
+  $entities['BioData_bundles'] = array (
+    'label' => 'Biological Data Type', //$voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
+    'entity class' => 'TripalBundle',
+    'controller class' => 'TripalBundleController',
+    'base table' => 'tripal_bundle',
+    'fieldable' => FALSE,
+    'bundle of' => 'BioData',
+    'exportable' => FALSE,
+    'entity keys' => array (
+      'id' => 'id',
+      'name' => 'bundle',
+      'label' => 'label'
+    ),
+    'access callback' => 'tripal_bundle_access',
+    'module' => 'tripal_entities',
+    // Enable the entity API's admin UI.
+    'admin ui' => array (
+      'path' => 'admin/structure/BioData',
+      'controller class' => 'TripalBundleUIController',
+      'file' => 'includes/TripalBundleUIController.inc',
+      'menu wildcard' => '%tripal_bundle',
+    )
+  );
 
-  // Get a list of published terms from 'tripal_term
+  // Dynamically add in the bundles. Bundles are alternative groups of fields
+  // or configuration associated with an entity type .We want to dynamically
+  // add the bundles to the entity.
   $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
   foreach ($published_terms as $term) {
 
-    // Bundles are alternative groups of fields or configuration
-    // associated with a base entity type.
-    // We want to dynamically add the bundles (or term types) to the entity.
     $cvterm = $term->cvterm_id;
-    $entity_type = 'BioData'; // $cvterm->dbxref_id->db_id->name;
-    $accession = $cvterm->dbxref_id->accession;
-    $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $accession;
-
+    $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
     $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
-    $entity_info[$entity_type]['bundles'][$bundle_id] = array (
+
+    $entities['BioData']['bundles'][$bundle_id] = array (
       'label' => $label,
       'admin' => array (
         'path' => 'admin/structure/BioData/manage/%tripal_bundle',
         'real path' => 'admin/structure/BioData/manage/' . $bundle_id,
-        'bundle argument' => 5,
+        'bundle argument' => 4,
         'access arguments' => array (
           'administer tripal data types'
         )
       )
     );
   }
+  return $entities;
 }
 
 /**
@@ -344,11 +330,11 @@ function tripal_bundle_load($bundle_type, $reset = FALSE) {
     ->execute()
     ->fetchObject();
 
-  // Load the tripal_bundle entity. These entities are always the same
-  // as an Tripal entity type but with a '_bundle' extension.
-  $entity = entity_load('BioData_bundles', array($bundle_types->id), array(), $reset);
-
-  return reset($entity);
+  if ($bundle_types) {
+    $entity = entity_load('BioData_bundles', array($bundle_types->id), array(), $reset);
+    return reset($entity);
+  }
+  return FALSE;
 }
 
 /**

+ 5 - 2
tripal_fields/tripal_fields.module

@@ -22,7 +22,9 @@ function tripal_fields_field_info() {
     ),
     'dbxref_id' => array(
       'label' => t('Cross-reference'),
-      'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference.  At a minimum, the database and accession must be provided.'),
+      'description' => t('This record can be cross-referenced with a record in
+          another online database. This field is intended for the most prominent
+          reference.  At a minimum, the database and accession must be provided.'),
       'default_widget' => 'tripal_fields_dbxref_id_widget',
       'default_formatter' => 'tripal_fields_dbxref_id_formatter',
       'settings' => array(),
@@ -73,7 +75,8 @@ function tripal_fields_field_info() {
     // in Chado.
     'kvproperty_adder' => array(
       'label' => t('Add a Property Type'),
-      'description' => t('This record may have any number of properties. Use this field to first add the type.'),
+      'description' => t('This record may have any number of properties. Use
+          this field to first add the type.'),
       'default_widget' => 'tripal_fields_kvproperty_adder_widget',
       'default_formatter' => 'tripal_fields_kvproperty_adder_formatter',
       'settings' => array(),