Browse Source

Fixed saving to Chado for new field classes. Also fixed bugs in relationship field

Stephen Ficklin 8 years ago
parent
commit
63c99dcf22

+ 4 - 6
tripal/includes/TripalEntityUIController.inc

@@ -435,13 +435,11 @@ function tripal_entity_form_ajax_callback($form, $form_state) {
          if (!preg_match('/^\d+$/', $delta)) {
          if (!preg_match('/^\d+$/', $delta)) {
            continue;
            continue;
          }
          }
-         if (array_key_exists('#field', $field_form)) {
+         $widget_type = $instance['widget']['type'];
+         if (tripal_load_include_field_class($widget_type)) {
            $field = $field_form['#field'];
            $field = $field_form['#field'];
-           $field_type = $field['type'];
-           if (class_exists($field_type)) {
-             $tfield = new $field_type($field, $instance);
-             $tfield->widgetFormSubmit($form, $form_state, $entity_type, $entity, $langcode, $delta);
-           }
+           $widget = new $widget_type($field, $instance);
+           $widget->submit($form, $form_state, $entity_type, $entity, $langcode, $delta);
          }
          }
        }
        }
      }
      }

+ 13 - 12
tripal_chado/includes/TripalFields/chado_linker__contact_widget.inc

@@ -13,9 +13,10 @@ class chado_linker__contact_widget extends TripalFieldWidget {
    */
    */
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+
     $entity = $form['#entity'];
     $entity = $form['#entity'];
     $field_name = $this->field['field_name'];
     $field_name = $this->field['field_name'];
-    
+
     // Get the FK column that links to the base table.
     // Get the FK column that links to the base table.
     $table_name = $this->field['settings']['chado_table'];
     $table_name = $this->field['settings']['chado_table'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
@@ -23,13 +24,13 @@ class chado_linker__contact_widget extends TripalFieldWidget {
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
-    
+
     // Get the field defaults.
     // Get the field defaults.
     $record_id = '';
     $record_id = '';
     $fkey_value = $element['#entity']->chado_record_id;
     $fkey_value = $element['#entity']->chado_record_id;
     $contact_id = '';
     $contact_id = '';
     $name = '';
     $name = '';
-    
+
     // If the field already has a value then it will come through the $items
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
     // array.  This happens when editing an existing record.
     if (count($items) > 0 and array_key_exists($delta, $items)) {
     if (count($items) > 0 and array_key_exists($delta, $items)) {
@@ -40,20 +41,20 @@ class chado_linker__contact_widget extends TripalFieldWidget {
         $name = $contact->name;
         $name = $contact->name;
       }
       }
     }
     }
-    
+
     $schema = chado_get_schema('contact');
     $schema = chado_get_schema('contact');
-    
+
     $widget['#table_name'] = $table_name;
     $widget['#table_name'] = $table_name;
     $widget['#fkey_field'] = $fkey;
     $widget['#fkey_field'] = $fkey;
     $widget['#theme'] = 'chado_linker__contact_widget';
     $widget['#theme'] = 'chado_linker__contact_widget';
     $widget['#prefix'] =  "<span id='$table_name-$delta'>";
     $widget['#prefix'] =  "<span id='$table_name-$delta'>";
     $widget['#suffix'] =  "</span>";
     $widget['#suffix'] =  "</span>";
-    
+
     $widget['value'] = array(
     $widget['value'] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
     );
-    
+
     $widget['chado-' . $table_name . '__' . $pkey] = array(
     $widget['chado-' . $table_name . '__' . $pkey] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $record_id,
       '#default_value' => $record_id,
@@ -66,7 +67,7 @@ class chado_linker__contact_widget extends TripalFieldWidget {
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $contact_id,
       '#default_value' => $contact_id,
     );
     );
-    
+
     $widget['name'] = array(
     $widget['name'] = array(
       '#type' => 'textfield',
       '#type' => 'textfield',
       '#title' => t('Contact'),
       '#title' => t('Contact'),
@@ -110,19 +111,19 @@ class chado_linker__contact_widget extends TripalFieldWidget {
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
     $field_name = $this->field['field_name'];
     $field_name = $this->field['field_name'];
-    
+
     // Get the field values.
     // Get the field values.
     $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
     $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
     $contact_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] : '';
     $contact_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] : '';
     $name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
     $name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
-    
+
     // If the user provided a name then we want to set the foreign key
     // If the user provided a name then we want to set the foreign key
     // value to be the chado_record_id
     // value to be the chado_record_id
     if ($name and !$contact_id) {
     if ($name and !$contact_id) {
       $contact = chado_generate_var('contact', array('name' => $name));
       $contact = chado_generate_var('contact', array('name' => $name));
       $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
       $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__contact_id'] = $contact->contact_id;
     }
     }
-    
+
     // In the widgetForm function we automatically add the foreign key
     // In the widgetForm function we automatically add the foreign key
     // record.  But if the user did not provide a contact we want to take
     // record.  But if the user did not provide a contact we want to take
     // it out so that the Chado field_storage infrastructure won't try to
     // it out so that the Chado field_storage infrastructure won't try to
@@ -130,7 +131,7 @@ class chado_linker__contact_widget extends TripalFieldWidget {
     if (!$name and !$contact_id) {
     if (!$name and !$contact_id) {
       $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
       $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
     }
     }
-    
+
     // If the user removed the contact from the contact_name field
     // If the user removed the contact from the contact_name field
     // then we want to clear out the rest of the hidden values.
     // then we want to clear out the rest of the hidden values.
     // Leave the primary key so the record can be deleted.
     // Leave the primary key so the record can be deleted.

+ 16 - 16
tripal_chado/includes/TripalFields/chado_linker__dbxref_widget.inc

@@ -17,7 +17,7 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
     $field_type = $this->field['type'];
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
     $field_column = $this->field['settings']['chado_column'];
-    
+
     // Get the FK column that links to the base table.
     // Get the FK column that links to the base table.
     $chado_table = $this->field['settings']['chado_table'];
     $chado_table = $this->field['settings']['chado_table'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
@@ -25,16 +25,16 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
-    
+
     // Get the field defaults.
     // Get the field defaults.
     $record_id = '';
     $record_id = '';
-    $fkey_value = $element['#entity']->chado_record_id;
+    $fkey_value = $element['#entity'] ? $element['#entity']->chado_record_id : '';
     $dbxref_id = '';
     $dbxref_id = '';
     $db_id = '';
     $db_id = '';
     $accession = '';
     $accession = '';
     $version = '';
     $version = '';
     $description = '';
     $description = '';
-    
+
     // If the field already has a value then it will come through the $items
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
     // array.  This happens when editing an existing record.
     if (count($items) > 0 and array_key_exists($delta, $items)) {
     if (count($items) > 0 and array_key_exists($delta, $items)) {
@@ -46,7 +46,7 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
       $version = tripal_get_field_item_keyval($items, $delta, 'version', $version);
       $version = tripal_get_field_item_keyval($items, $delta, 'version', $version);
       $description = tripal_get_field_item_keyval($items, $delta, 'description', $description);
       $description = tripal_get_field_item_keyval($items, $delta, 'description', $description);
     }
     }
-    
+
     // Check $form_state['values'] to see if an AJAX call set the values.
     // Check $form_state['values'] to see if an AJAX call set the values.
     if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
     if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
       $record_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $pkey];
       $record_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $pkey];
@@ -57,23 +57,23 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
       $version = $form_state['values'][$field_name]['und'][$delta]['version'];
       $version = $form_state['values'][$field_name]['und'][$delta]['version'];
       $description = $form_state['values'][$field_name]['und'][$delta]['description'];
       $description = $form_state['values'][$field_name]['und'][$delta]['description'];
     }
     }
-    
+
     $schema = chado_get_schema('dbxref');
     $schema = chado_get_schema('dbxref');
     $options = tripal_get_db_select_options();
     $options = tripal_get_db_select_options();
-    
+
     $widget['#table_name'] = $chado_table;
     $widget['#table_name'] = $chado_table;
     $widget['#fkey_field'] = $fkey;
     $widget['#fkey_field'] = $fkey;
     //$widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
     //$widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
     $widget['#theme'] = 'chado_linker__dbxref_widget';
     $widget['#theme'] = 'chado_linker__dbxref_widget';
     $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id-$delta'>";
     $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id-$delta'>";
     $widget['#suffix'] =  "</span>";
     $widget['#suffix'] =  "</span>";
-    
-    
+
+
     $widget['value'] = array(
     $widget['value'] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
     );
-    
+
     $widget['chado-' . $field_table . '__' . $pkey] = array(
     $widget['chado-' . $field_table . '__' . $pkey] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $record_id,
       '#default_value' => $record_id,
@@ -163,18 +163,18 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
-    
-    
+
+
     // Get the field values.
     // Get the field values.
     foreach ($items as $delta => $values) {
     foreach ($items as $delta => $values) {
-    
+
       // Get the field values.
       // Get the field values.
       $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
       $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
       $db_id = $values['db_id'];
       $db_id = $values['db_id'];
       $accession = $values['accession'];
       $accession = $values['accession'];
       $version = $values['version'];
       $version = $values['version'];
       $description = $values['description'];
       $description = $values['description'];
-    
+
       // Make sure that if a database ID is provided that an accession is also
       // Make sure that if a database ID is provided that an accession is also
       // provided.  Here we use the form_set_error function rather than the
       // provided.  Here we use the form_set_error function rather than the
       // form_error function because the form_error will add a red_highlight
       // form_error function because the form_error will add a red_highlight
@@ -219,14 +219,14 @@ class chado_linker__dbxref_widget extends TripalFieldWidget {
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
-    
+
     // Get the field values.
     // Get the field values.
     $dbxref_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] : '';
     $dbxref_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__dbxref_id'] : '';
     $db_id = isset($form_state['values'][$field_name][$langcode][$delta]['db_id']) ? $form_state['values'][$field_name][$langcode][$delta]['db_id'] : '';
     $db_id = isset($form_state['values'][$field_name][$langcode][$delta]['db_id']) ? $form_state['values'][$field_name][$langcode][$delta]['db_id'] : '';
     $accession = isset($form_state['values'][$field_name][$langcode][$delta]['accession']) ? $form_state['values'][$field_name][$langcode][$delta]['accession'] : '';
     $accession = isset($form_state['values'][$field_name][$langcode][$delta]['accession']) ? $form_state['values'][$field_name][$langcode][$delta]['accession'] : '';
     $version = isset($form_state['values'][$field_name][$langcode][$delta]['version']) ? $form_state['values'][$field_name][$langcode][$delta]['version'] : '';
     $version = isset($form_state['values'][$field_name][$langcode][$delta]['version']) ? $form_state['values'][$field_name][$langcode][$delta]['version'] : '';
     $description = isset($form_state['values'][$field_name][$langcode][$delta]['description']) ? $form_state['values'][$field_name][$langcode][$delta]['description'] : '';
     $description = isset($form_state['values'][$field_name][$langcode][$delta]['description']) ? $form_state['values'][$field_name][$langcode][$delta]['description'] : '';
-    
+
     // If the dbxref_id does not match the db_id + accession then the user
     // If the dbxref_id does not match the db_id + accession then the user
     // has selected a new dbxref record and we need to update the hidden
     // has selected a new dbxref record and we need to update the hidden
     // value accordingly.
     // value accordingly.

+ 48 - 52
tripal_chado/includes/TripalFields/chado_linker__relationship.inc

@@ -76,19 +76,19 @@ class chado_linker__relationship extends TripalField {
    */
    */
   public function load($entity, $details = array()) {
   public function load($entity, $details = array()) {
     $settings = $this->field['settings'];
     $settings = $this->field['settings'];
-    
+
     $record = $details['record'];
     $record = $details['record'];
-    
+
     $field_name = $this->field['field_name'];
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
     $field_column = $this->field['settings']['chado_column'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
-    
+
     // Get the PKey for this table
     // Get the PKey for this table
     $schema = chado_get_schema($field_table);
     $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
-    
+
     // Get the Pkeys for the subject and object tables
     // Get the Pkeys for the subject and object tables
     $subject_fkey_table = '';
     $subject_fkey_table = '';
     $object_fkey_table = '';
     $object_fkey_table = '';
@@ -107,19 +107,19 @@ class chado_linker__relationship extends TripalField {
     $object_schema = chado_get_schema($object_fkey_table);
     $object_schema = chado_get_schema($object_fkey_table);
     $subject_pkey = $subject_schema['primary key'][0];
     $subject_pkey = $subject_schema['primary key'][0];
     $object_pkey = $object_schema['primary key'][0];
     $object_pkey = $object_schema['primary key'][0];
-    
+
     // Get the FK that links to the base record.
     // Get the FK that links to the base record.
     $schema = chado_get_schema($field_table);
     $schema = chado_get_schema($field_table);
     $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
     $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
     $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
     $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
-    
+
     // Set some defaults for the empty record.
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
     $entity->{$field_name}['und'][0] = array(
       'value' => array(),
       'value' => array(),
-      $field_table . '__' . $pkey => '',
-      $field_table . '__subject_id' => '',
-      $field_table . '__object_id' => '',
-      $field_table . '__type_id' => '',
+      'chado-' . $field_table . '__' . $pkey => '',
+      'chado-' . $field_table . '__subject_id' => '',
+      'chado-' . $field_table . '__object_id' => '',
+      'chado-' . $field_table . '__type_id' => '',
       // These elements don't need to follow the naming scheme above
       // These elements don't need to follow the naming scheme above
       // becasue we don't need the chado_field_storage to try and
       // becasue we don't need the chado_field_storage to try and
       // save these values.
       // save these values.
@@ -127,21 +127,21 @@ class chado_linker__relationship extends TripalField {
       'subject_name' => '',
       'subject_name' => '',
       'type_name' => '',
       'type_name' => '',
     );
     );
-    
+
     // If the table has rank and value fields then add those to the default
     // If the table has rank and value fields then add those to the default
     // value array.
     // value array.
     if (array_key_exists('value', $schema['fields'])) {
     if (array_key_exists('value', $schema['fields'])) {
-      $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
+      $entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
     }
     }
     if (array_key_exists('rank', $schema['fields'])) {
     if (array_key_exists('rank', $schema['fields'])) {
-      $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
+      $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
     }
     }
-    
+
     // If we have no record then just return.
     // If we have no record then just return.
     if (!$record) {
     if (!$record) {
       return;
       return;
     }
     }
-    
+
     // Expand the object to include the relationships.
     // Expand the object to include the relationships.
     $options = array(
     $options = array(
       'return_array' => 1,
       'return_array' => 1,
@@ -168,11 +168,7 @@ class chado_linker__relationship extends TripalField {
     }
     }
     $srelationships = null;
     $srelationships = null;
     $orelationships = null;
     $orelationships = null;
-    if ($rel_table == 'organism_relationship') {
-      $srelationships = $record->$rel_table->subject_organism_id;
-      $orelationships = $record->$rel_table->object_organism_id;
-    }
-    else if ($rel_table == 'nd_reagent_relationship') {
+    if ($rel_table == 'nd_reagent_relationship') {
       $srelationships = $record->$rel_table->subject_reagent_id;
       $srelationships = $record->$rel_table->subject_reagent_id;
       $orelationships = $record->$rel_table->object_reagent_id;
       $orelationships = $record->$rel_table->object_reagent_id;
     }
     }
@@ -184,7 +180,7 @@ class chado_linker__relationship extends TripalField {
       $srelationships = $record->$rel_table->subject_id;
       $srelationships = $record->$rel_table->subject_id;
       $orelationships = $record->$rel_table->object_id;
       $orelationships = $record->$rel_table->object_id;
     }
     }
-    
+
     $i = 0;
     $i = 0;
     if ($orelationships) {
     if ($orelationships) {
       foreach ($orelationships as $relationship) {
       foreach ($orelationships as $relationship) {
@@ -222,31 +218,31 @@ class chado_linker__relationship extends TripalField {
         $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
         $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
             $subject_name . ', ' . $verb . ' '  . $rel_type_clean . ' this '  .
             $subject_name . ', ' . $verb . ' '  . $rel_type_clean . ' this '  .
             $object_type . '.';
             $object_type . '.';
-    
+
             $entity->{$field_name}['und'][$i]['semantic_web'] = array(
             $entity->{$field_name}['und'][$i]['semantic_web'] = array(
               'type' => $rel_acc,
               'type' => $rel_acc,
               'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
               'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
               'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
               'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
             );
             );
-    
-            $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
-            $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
-            $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
-            $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
-    
+
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
+
             $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
             $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
             $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name  . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name  . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
             if (array_key_exists('value', $schema['fields'])) {
             if (array_key_exists('value', $schema['fields'])) {
-              $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
+              $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
             }
             }
             if (array_key_exists('rank', $schema['fields'])) {
             if (array_key_exists('rank', $schema['fields'])) {
-              $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
+              $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
             }
             }
             $i++;
             $i++;
       }
       }
     }
     }
-    
+
     if ($srelationships) {
     if ($srelationships) {
       foreach ($srelationships as $relationship) {
       foreach ($srelationships as $relationship) {
         $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
         $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
@@ -283,29 +279,29 @@ class chado_linker__relationship extends TripalField {
         $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This  ' .
         $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This  ' .
             $subject_type . ' ' . $verb . ' '  . $rel_type_clean . ' the '  .
             $subject_type . ' ' . $verb . ' '  . $rel_type_clean . ' the '  .
             $object_type . ', ' . $object_name . '.';
             $object_type . ', ' . $object_name . '.';
-    
-    
+
+
             $entity->{$field_name}['und'][$i]['semantic_web'] = array(
             $entity->{$field_name}['und'][$i]['semantic_web'] = array(
               'type' => $rel_acc,
               'type' => $rel_acc,
               'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
               'subject' => $relationship->subject_id->type_id->dbxref_id->db_id->name . ':' . $relationship->subject_id->type_id->dbxref_id->accession,
               'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
               'object' => $relationship->object_id->type_id->dbxref_id->db_id->name . ':' . $relationship->object_id->type_id->dbxref_id->accession,
             );
             );
-    
-    
-            $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
-            $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
-            $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
-            $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
-    
+
+
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
+            $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__object_id'] = $relationship->object_id->$object_pkey;
+
             $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
             $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
             $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name  . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['subject_name'] = $relationship->subject_id->name  . ' [id: ' . $relationship->subject_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name  . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
             $entity->{$field_name}['und'][$i]['object_name'] = $relationship->object_id->name  . ' [id: ' . $relationship->object_id->$fkey_rcolumn . ']';
-    
+
             if (array_key_exists('value', $schema['fields'])) {
             if (array_key_exists('value', $schema['fields'])) {
-              $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
+              $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
             }
             }
             if (array_key_exists('rank', $schema['fields'])) {
             if (array_key_exists('rank', $schema['fields'])) {
-              $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
+              $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
             }
             }
             $i++;
             $i++;
       }
       }
@@ -355,17 +351,17 @@ class chado_linker__relationship extends TripalField {
       default:
       default:
         $verb = 'is';
         $verb = 'is';
     }
     }
-  
+
     return $verb;
     return $verb;
   }
   }
-  
+
   /**
   /**
    *
    *
    * @see TripalField::settingsForm()
    * @see TripalField::settingsForm()
    */
    */
   public function settingsForm($has_data) {
   public function settingsForm($has_data) {
     $element = parent::instanceSettingsForm();
     $element = parent::instanceSettingsForm();
-    
+
     //$element = parent::instanceSettingsForm();
     //$element = parent::instanceSettingsForm();
     $element['relationships'] = array(
     $element['relationships'] = array(
       '#type' => 'fieldset',
       '#type' => 'fieldset',
@@ -393,7 +389,7 @@ class chado_linker__relationship extends TripalField {
         could use to specify relationship types. With this option any term in .
         could use to specify relationship types. With this option any term in .
         the vocabulary can be used for the relationship type. You may select
         the vocabulary can be used for the relationship type. You may select
         more than one vocabulary.'),
         more than one vocabulary.'),
-    
+
     );
     );
     $element['relationships']['option1_vocabs'] = array(
     $element['relationships']['option1_vocabs'] = array(
       '#type' => 'select',
       '#type' => 'select',
@@ -403,7 +399,7 @@ class chado_linker__relationship extends TripalField {
       '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
       '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
       // TODO add ajax here so that the relationship autocomplete below works
       // TODO add ajax here so that the relationship autocomplete below works
     );
     );
-    
+
     $element['relationships']['option2'] = array(
     $element['relationships']['option2'] = array(
       '#type' => 'item',
       '#type' => 'item',
       '#title' => '<b>Option #2</b>',
       '#title' => '<b>Option #2</b>',
@@ -445,7 +441,7 @@ class chado_linker__relationship extends TripalField {
       '#type' => 'textarea',
       '#type' => 'textarea',
       '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
       '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
     );
     );
-    
+
     return $element;
     return $element;
   }
   }
   /**
   /**
@@ -457,7 +453,7 @@ class chado_linker__relationship extends TripalField {
     // Get relationships settings
     // Get relationships settings
     $settings = $form_state['values']['instance']['settings']['relationships'];
     $settings = $form_state['values']['instance']['settings']['relationships'];
     $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
     $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
-    
+
     // Make sure only one option is selected
     // Make sure only one option is selected
     $option1test = $settings['option1_vocabs'];
     $option1test = $settings['option1_vocabs'];
     $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
     $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
@@ -473,7 +469,7 @@ class chado_linker__relationship extends TripalField {
               );
               );
           return;
           return;
         }
         }
-    
+
         // For option3, make sure the supplied types are valid cvterms
         // For option3, make sure the supplied types are valid cvterms
         if ($option3) {
         if ($option3) {
           $rel_types = explode(PHP_EOL, $settings['relationship_types']);
           $rel_types = explode(PHP_EOL, $settings['relationship_types']);
@@ -546,7 +542,7 @@ class chado_linker__relationship extends TripalField {
             }
             }
           }
           }
         }
         }
-    
+
         // For option2: Make sure the parent term is a valid cvterm
         // For option2: Make sure the parent term is a valid cvterm
         if ($option2) {
         if ($option2) {
           $cv_id = $settings['option2_vocab'];
           $cv_id = $settings['option2_vocab'];

+ 134 - 118
tripal_chado/includes/TripalFields/chado_linker__relationship_widget.inc

@@ -13,131 +13,141 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
    */
    */
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+
+    // Get the field settings.
+    $entity = $form['#entity'];
     $field_name = $this->field['field_name'];
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
     $field_column = $this->field['settings']['chado_column'];
-    
-    // Get the instance settings
-    $instance = $this->instance;
-    $settings = $instance['settings']['relationships'];
-    
-    $option1_vocabs = $settings['option1_vocabs'];
-    $option2_vocab  = $settings['option2_vocab'];
-    $option2_parent = $settings['option2_parent'];
-    $option3_rtypes = $settings['relationship_types'];
-    
-    // For testing if there are selected vocabs for option1 we'll copy the
-    // contents in a special variable for later.
-    $option1_test   = $option1_vocabs;
-    
+    $base_table = $this->field['settings']['base_table'];
+
     // Get the FK column that links to the base table.
     // Get the FK column that links to the base table.
-    $chado_table = $this->field['settings']['chado_table'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
-    $schema = chado_get_schema($chado_table);
+    $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
     $fkey = $fkeys[0];
-    
+
+    // Get the instance settings. There are three options for how this widget
+    // will be displayed. Those are controlled in the instance settings
+    // of the field.
+    // Option 1:  relationship types are limited to a specific vocabulary.
+    // Option 2:  relationship types are limited to a subset of one vocabulary.
+    // Option 3:  relationship types are limited to a predefined set.
+    $instance = $this->instance;
+    $settings = '';
+    $option1_vocabs = '';
+    $option2_parent = '';
+    $option2_vocab = '';
+    $option3_rtypes  = '';
+    if (array_key_exists('relationships', $instance)) {
+      $settings = $instance['settings']['relationships'];
+      $option1_vocabs = $settings['option1_vocabs'];
+      $option2_vocab  = $settings['option2_vocab'];
+      $option2_parent = $settings['option2_parent'];
+      $option3_rtypes = $settings['relationship_types'];
+    }
+
+    // For testing if there are selected vocabs for option1 we'll copy the
+    // contents in a special variable for later.
+    $option1_test = $option1_vocabs;
+
     // Get the field defaults.
     // Get the field defaults.
     $record_id = '';
     $record_id = '';
-    $fkey_value = $element['#entity'] ? $element['#entity']->chado_record_id : '';
     $subject_id = '';
     $subject_id = '';
-    $subject_uniquename = '';
-    $type_id = '';
-    $type = '';
     $object_id = '';
     $object_id = '';
-    $object_uniquename = '';
+    $type_id = '';
     $value = '';
     $value = '';
     $rank = '';
     $rank = '';
-    
+    $subject_uniquename = '';
+    $object_uniquename = '';
+    $type = '';
+
     // Handle special cases
     // Handle special cases
     $subject_id_key = 'subject_id';
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
     $object_id_key = 'object_id';
-    if ($chado_table == 'organism_relationship') {
-      $subject_id_key = 'subject_organism_id';
-      $object_id_key = 'object_organism_id';
-    }
-    else if ($chado_table == 'nd_reagent_relationship') {
+    if ($field_table == 'nd_reagent_relationship') {
       $subject_id_key = 'subject_reagent_id';
       $subject_id_key = 'subject_reagent_id';
       $object_id_key = 'object_reagent_id';
       $object_id_key = 'object_reagent_id';
     }
     }
-    else if ($chado_table == 'project_relationship') {
+    else if ($field_table == 'project_relationship') {
       $subject_id_key = 'subject_project_id';
       $subject_id_key = 'subject_project_id';
       $object_id_key = 'object_project_id';
       $object_id_key = 'object_project_id';
     }
     }
-    
+
     // If the field already has a value then it will come through the $items
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
     // array.  This happens when editing an existing record.
-    if (array_key_exists($delta, $items)) {
-      $record_id = isset($items[$delta][$field_table . '__' . $pkey]) ? $items[$delta][$field_table . '__' . $pkey] : '';
-      $subject_id = isset($items[$delta][$field_table . '__' . $subject_id_key]) ? $items[$delta][$field_table . '__' . $subject_id_key] : '';
-      $type_id = isset($items[$delta][$field_table . '__type_id']) ? $items[$delta][$field_table . '__type_id'] : '';
-      $object_id = isset($items[$delta][$field_table . '__' . $object_id_key]) ? $items[$delta][$field_table . '__' . $object_id_key] : '';
-    
-      if (isset($items[$delta][$field_table . '__value'])) {
-        $value = $items[$delta][$field_table . '__value'];
+    if (count($items) > 0 and array_key_exists($delta, $items)) {
+      // Check for element values that correspond to fields in the Chado table.
+      $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
+      $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
+      $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
+      $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
+      // Not all Chado tables have a value and rank.  So we'll only get
+      // those if applicable.
+      if (array_key_exists('value', $schema['fields'])) {
+        $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
       }
       }
-      if (isset($items[$delta][$field_table . '__rank'])) {
-        $rank = $items[$delta][$field_table . '__rank'];
+      if (array_key_exists('rank', $schema['fields'])) {
+        $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
       }
       }
-    
-      $object_uniquename = isset($items[$delta]['object_name']) ? $items[$delta]['object_name'] : '';
-      $subject_uniquename = isset($items[$delta]['subject_name']) ? $items[$delta]['subject_name'] : '';
-      $type = isset($items[$delta]['type_name']) ? $items[$delta]['type_name'] : '';
+      // Get element values added to help support insert/updates.
+      $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
+      $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
+      $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
     }
     }
-    
+
     // Check $form_state['values'] to see if an AJAX call set the values.
     // Check $form_state['values'] to see if an AJAX call set the values.
     if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
     if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
-      //       $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
-      //       $subject_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__subject_id');
-      //       $subject_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'subject_name');
-      //       $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__type_id');
-      //       $type = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'type_name');
-      //       $object_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__object_id');
-      //       $object_uniquename = tripal_chado_get_field_form_values($field_name, $form_state, $delta, 'object_name');
-      //       $value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__value');
-      //       $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__rank');
+      $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
+      $subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
+      $object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
+      $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
+      if (array_key_exists('value', $schema['fields'])) {
+        $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
+      }
+      if (array_key_exists('rank', $schema['fields'])) {
+        $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
+      }
+      $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
+      $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
+      $type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
     }
     }
-    
-    $widget['#table_name'] = $chado_table;
-    
-    $widget['#fkeys'] = $schema['foreign keys'];
-    $widget['#base_table'] = $base_table;
-    $widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
+
     //$widget['#element_validate'] = array('chado_linker__relationship_validate');
     //$widget['#element_validate'] = array('chado_linker__relationship_validate');
     $widget['#theme'] = 'chado_linker__relationship_widget';
     $widget['#theme'] = 'chado_linker__relationship_widget';
-    $widget['#prefix'] =  "<span id='$chado_table-$delta'>";
+    $widget['#prefix'] =  "<span id='$field_table-$delta'>";
     $widget['#suffix'] =  "</span>";
     $widget['#suffix'] =  "</span>";
-    
+
     $widget['value'] = array(
     $widget['value'] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
     );
-    $widget[$field_table . '__' . $pkey] = array(
+    $widget['chado-' . $field_table . '__' . $pkey] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $record_id,
       '#default_value' => $record_id,
     );
     );
-    $widget[$field_table . '__' . $subject_id_key] = array(
+    $widget['chado-' . $field_table . '__' . $subject_id_key] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $subject_id,
       '#default_value' => $subject_id,
     );
     );
-    $widget[$field_table . '__type_id'] = array(
+    $widget['chado-' . $field_table . '__type_id'] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $type_id,
       '#default_value' => $type_id,
     );
     );
-    $widget[$field_table . '__' . $object_id_key] = array(
+    $widget['chado-' . $field_table . '__' . $object_id_key] = array(
       '#type' => 'value',
       '#type' => 'value',
       '#default_value' => $object_id,
       '#default_value' => $object_id,
     );
     );
     if (array_key_exists('value', $schema['fields'])) {
     if (array_key_exists('value', $schema['fields'])) {
-      $widget[$field_table . '__value'] = array(
+      $widget['chado-' . $field_table . '__value'] = array(
         '#type' => 'value',
         '#type' => 'value',
         '#default_value' => $value,
         '#default_value' => $value,
       );
       );
     }
     }
     if (array_key_exists('rank', $schema['fields'])) {
     if (array_key_exists('rank', $schema['fields'])) {
-      $widget[$field_table . '__rank'] = array(
+      $widget['chado-' . $field_table . '__rank'] = array(
         '#type' => 'value',
         '#type' => 'value',
         '#default_value' => $rank,
         '#default_value' => $rank,
       );
       );
@@ -151,8 +161,8 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
       '#size' => 35,
       '#size' => 35,
       '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
       '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
     );
     );
-    
-    // Getting default values
+
+    // Getting default values for the relationship type element.
     $default_voc = '';
     $default_voc = '';
     if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
     if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
       $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
       $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
@@ -161,7 +171,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
     if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
     if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
       $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
       $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
     }
     }
-    
+
     $default_type_id = $type_id;
     $default_type_id = $type_id;
     if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
     if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
       $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
       $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
@@ -203,7 +213,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
         'name' => $option2_parent
         'name' => $option2_parent
       );
       );
       $parent_term = tripal_get_cvterm($values);
       $parent_term = tripal_get_cvterm($values);
-    
+
       // If the term wasn't found then see if it's a synonym.
       // If the term wasn't found then see if it's a synonym.
       if(!$parent_term) {
       if(!$parent_term) {
         $values = array(
         $values = array(
@@ -217,16 +227,14 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
         }
         }
       }
       }
       // Get the child terms of the parent term found above.
       // Get the child terms of the parent term found above.
-      $sql =
-      "SELECT
-           subject_id,
-           (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
-         FROM {cvtermpath}
-         WHERE
-           object_id = :parent_cvterm_id
-         AND
-           cv_id = :parent_cv_id
-         ORDER BY name
+      $sql = "
+        SELECT subject_id,
+          (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
+        FROM {cvtermpath}
+        WHERE
+          object_id = :parent_cvterm_id AND
+          cv_id = :parent_cv_id
+        ORDER BY name
        ";
        ";
       $args = array(
       $args = array(
         ':parent_cvterm_id' => $parent_term->cvterm_id,
         ':parent_cvterm_id' => $parent_term->cvterm_id,
@@ -288,7 +296,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
         '#default_value' => $cv_id,
         '#default_value' => $cv_id,
         '#ajax' => array(
         '#ajax' => array(
           'callback' => "chado_linker__relationship_widget_form_ajax_callback",
           'callback' => "chado_linker__relationship_widget_form_ajax_callback",
-          'wrapper' => "$chado_table-$delta",
+          'wrapper' => "$field_table-$delta",
           'effect' => 'fade',
           'effect' => 'fade',
           'method' => 'replace'
           'method' => 'replace'
         ),
         ),
@@ -304,7 +312,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
         );
         );
       }
       }
     }
     }
-    
+
     $widget['object_name'] = array(
     $widget['object_name'] = array(
       '#type' => 'textfield',
       '#type' => 'textfield',
       '#title' => t('Object'),
       '#title' => t('Object'),
@@ -332,18 +340,14 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
     $field_table = $this->field['settings']['chado_table'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
     $field_column = $this->field['settings']['chado_column'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
-    
+
     $schema = chado_get_schema($field_table);
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];
     $fkeys = $schema['foreign keys'];
-    
+
     // Handle special cases
     // Handle special cases
     $subject_id_key = 'subject_id';
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
     $object_id_key = 'object_id';
-    if ($field_table == 'organism_relationship') {
-      $subject_id_key = 'subject_organism_id';
-      $object_id_key = 'object_organism_id';
-    }
-    else if ($field_table == 'nd_reagent_relationship') {
+    if ($field_table == 'nd_reagent_relationship') {
       $subject_id_key = 'subject_reagent_id';
       $subject_id_key = 'subject_reagent_id';
       $object_id_key = 'object_reagent_id';
       $object_id_key = 'object_reagent_id';
     }
     }
@@ -351,22 +355,22 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
       $subject_id_key = 'subject_project_id';
       $subject_id_key = 'subject_project_id';
       $object_id_key = 'object_project_id';
       $object_id_key = 'object_project_id';
     }
     }
-    
+
     foreach ($items as $delta => $item) {
     foreach ($items as $delta => $item) {
-      $subject_id = $item[$field_table . '__' . $subject_id_key];
-      $object_id = $item[ $field_table . '__' . $object_id_key];
-      $type_id = $item[$field_table . '__type_id'];
-      $type_id = isset($item['type_id']) ? $item['type_id'] : $type_id;
+      $subject_id = $item['chado' . $field_table . '__' . $subject_id_key];
+      $object_id = $item['chado' . $field_table . '__' . $object_id_key];
+      $type_id = $item['chado' . $field_table . '__type_id'];
+      $type_id = isset($item['type_id']) ? $item['chado' . $field_table . '__type_id'] : $type_id;
       $type_name = isset($item['type_name']) ? $item['type_name'] : '';
       $type_name = isset($item['type_name']) ? $item['type_name'] : '';
       $subject_name = $item['subject_name'];
       $subject_name = $item['subject_name'];
       $object_name = $item['object_name'];
       $object_name = $item['object_name'];
-    
-    
+
+
       // If the row is empty then just continue, there's nothing to validate.
       // If the row is empty then just continue, there's nothing to validate.
       if (!$type_id and !$type_name and !$subject_name and !$object_name) {
       if (!$type_id and !$type_name and !$subject_name and !$object_name) {
         continue;
         continue;
       }
       }
-    
+
       // Make sure we have values for all of the fields.
       // Make sure we have values for all of the fields.
       $form_error = FALSE;
       $form_error = FALSE;
       if (!$type_name && !$type_id) {
       if (!$type_name && !$type_id) {
@@ -390,9 +394,9 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
       if ($form_error) {
       if ($form_error) {
         continue;
         continue;
       }
       }
-    
+
       // Before submitting this form we need to make sure that our subject_id and
       // Before submitting this form we need to make sure that our subject_id and
-      // object_ids are real values.  There are two ways to get the value, either
+      // object_ids are real records.  There are two ways to get the record, either
       // just with the text value or with an [id: \d+] string embedded.  If the
       // just with the text value or with an [id: \d+] string embedded.  If the
       // later we will pull it out.
       // later we will pull it out.
       $subject_id = '';
       $subject_id = '';
@@ -427,7 +431,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
           }
           }
         }
         }
       }
       }
-    
+
       // Now check for a matching object.
       // Now check for a matching object.
       $object_id = '';
       $object_id = '';
       $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
       $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
@@ -461,7 +465,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
           }
           }
         }
         }
       }
       }
-    
+
       // Make sure that either our object or our subject refers to the base record.
       // Make sure that either our object or our subject refers to the base record.
       if ($entity) {
       if ($entity) {
         $chado_record_id = $entity->chado_record_id;
         $chado_record_id = $entity->chado_record_id;
@@ -471,7 +475,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
             'message' =>  t("Either the subject or the object in the relationship must refer to this record."),
             'message' =>  t("Either the subject or the object in the relationship must refer to this record."),
           );
           );
         }
         }
-    
+
         // Make sure that the object and subject are not both the same thing.
         // Make sure that the object and subject are not both the same thing.
         if ($object_id == $subject_id) {
         if ($object_id == $subject_id) {
           $errors[$this->field['field_name']][$langcode][$delta][] = array(
           $errors[$this->field['field_name']][$langcode][$delta][] = array(
@@ -495,23 +499,35 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
     $field_column = $this->field['settings']['chado_column'];
     $field_column = $this->field['settings']['chado_column'];
     $base_table = $this->field['settings']['base_table'];
     $base_table = $this->field['settings']['base_table'];
     $chado_record_id = $entity->chado_record_id;
     $chado_record_id = $entity->chado_record_id;
-    
+
     $schema = chado_get_schema($field_table);
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];
     $fkeys = $schema['foreign keys'];
-    
+
+    // Handle special cases
+    $subject_id_key = 'subject_id';
+    $object_id_key = 'object_id';
+    if ($field_table == 'nd_reagent_relationship') {
+      $subject_id_key = 'subject_reagent_id';
+      $object_id_key = 'object_reagent_id';
+    }
+    else if ($field_table == 'project_relationship') {
+      $subject_id_key = 'subject_project_id';
+      $object_id_key = 'object_project_id';
+    }
+
     $type_name = array_key_exists('type_name', $item) ? $item['type_name'] : '';
     $type_name = array_key_exists('type_name', $item) ? $item['type_name'] : '';
-    $subject_id = $form_state['values'][$field_name][$langcode][$delta][$field_table . '__subject_id'];
-    $object_id = $form_state['values'][$field_name][$langcode][$delta][ $field_table . '__object_id'];
-    $type_id = $form_state['values'][$field_name][$langcode][$delta][$field_table . '__type_id'];
-    
+    $subject_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key];
+    $object_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key];
+    $type_id = $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'];
+
     $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
     $subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
     $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
     $object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
-    
+
     // If the row is empty then skip this one, there's nothing to validate.
     // If the row is empty then skip this one, there's nothing to validate.
     if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
     if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
       return;
       return;
     }
     }
-    
+
     // Get the subject ID.
     // Get the subject ID.
     $subject_id = '';
     $subject_id = '';
     $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
     $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
@@ -524,7 +540,7 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
       $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
       $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
       $subject_id = $subject[0]->$fkey_rcolumn;
       $subject_id = $subject[0]->$fkey_rcolumn;
     }
     }
-    
+
     // Get the object ID.
     // Get the object ID.
     $object_id = '';
     $object_id = '';
     $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
     $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
@@ -537,12 +553,12 @@ class chado_linker__relationship_widget extends TripalFieldWidget {
       $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
       $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
       $object_id = $object[0]->$fkey_rcolumn;
       $object_id = $object[0]->$fkey_rcolumn;
     }
     }
-    
+
     // Set the IDs according to the values that were determined above.
     // Set the IDs according to the values that were determined above.
-    $form_state['values'][$field_name][$langcode][$delta][$field_table . '__subject_id'] = $subject_id;
-    $form_state['values'][$field_name][$langcode][$delta][$field_table . '__object_id'] = $object_id;
-    $form_state['values'][$field_name][$langcode][$delta][$field_table . '__type_id'] = $type_name;
-    $form_state['values'][$field_name][$langcode][$delta][$field_table . '__rank'] = $item['_weight'];
+    $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $subject_id_key] = $subject_id;
+    $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__' . $object_id_key] = $object_id;
+    $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__type_id'] = $type_name;
+    $form_state['values'][$field_name][$langcode][$delta]['chado' . $field_table . '__rank'] = $item['_weight'];
   }
   }
 }
 }
 
 

+ 89 - 94
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -44,9 +44,9 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
   $field_vals = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
   $field_vals = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
 
 
   // First, write the record for the base table.  If we have a record id then
   // First, write the record for the base table.  If we have a record id then
-  // this is an upate and we need to set the primary key.  If not, then this
+  // this is an update and we need to set the primary key.  If not, then this
   // is an insert and we need to set the type_id if the table supports it.
   // is an insert and we need to set the type_id if the table supports it.
-  $values = $field_vals[$base_table][0];
+  $values = $field_vals[$base_table];
   if ($record_id) {
   if ($record_id) {
     $values[$base_pkey] = $record_id;
     $values[$base_pkey] = $record_id;
   }
   }
@@ -109,93 +109,72 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
  *   The unique record ID.
  *   The unique record ID.
  */
  */
 function tripal_chado_field_storage_write_table($table_name, $values) {
 function tripal_chado_field_storage_write_table($table_name, $values) {
-   $schema = chado_get_schema($table_name);
-   $fkeys = $schema['foreign keys'];
-   $pkey = $schema['primary key'][0];
-
-
-//    // Before inserting or updating this table, recurse if there are any
-//    // nested FK array values.
-//    foreach ($values as $column => $value) {
-//      // If this value is an array then it must be a FK... let's recurse.
-//      if (is_array($value)) {
-
-//        // Find the name of the FK table for this column.
-//        $fktable_name = '';
-//        foreach ($fkeys as $fktable => $details) {
-//          foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
-//            if ($fkey_lcolumn == $column) {
-//              $fktable_name = $fktable;
-//            }
-//          }
-//        }
-
-//        // Recurse on this recod.
-//        $record_id = tripal_chado_field_storage_write_table($fktable_name, $values[$column]);
-//        $values[$column] = $record_id;
-//      }
-//    }
-
-   // Fields with a cardinality greater than 1 will often submit an
-   // empty form.  We want to remove these empty submissions.  We can detect
-   // them if all of the fields are empty.
-   $num_empty = 0;
-   foreach ($values as $column => $value) {
-     if (!$value) {
-       $num_empty++;
-     }
-   }
-   if ($num_empty == count(array_keys($values))) {
-     return '';
-   }
-   // If the primary key column has a value but all other values are empty then
-   // this is a delete.
-   if (array_key_exists($pkey, $values) and $values[$pkey]) {
-     $num_vals = 0;
-     foreach ($values as $value) {
-       if ($value) {
-         $num_vals++;
-       }
-     }
-     if ($num_vals == 1) {
-       $new_vals[$pkey] = $values[$pkey];
-       if (!chado_delete_record($table_name, $new_vals)) {
-         throw new Exception('Could not delete record from table: "' . $table_name . '".');
-       }
-       return '';
-     }
-   }
+  $schema = chado_get_schema($table_name);
+  $fkeys = $schema['foreign keys'];
+  $pkey = $schema['primary key'][0];
+
+
+  // Fields with a cardinality greater than 1 will often submit an
+  // empty form.  We want to remove these empty submissions.  We can detect
+  // them if all of the fields are empty.
+  $num_empty = 0;
+  foreach ($values as $column => $value) {
+    if (!$value) {
+      $num_empty++;
+    }
+  }
+  if ($num_empty == count(array_keys($values))) {
+    return '';
+  }
 
 
-   // If the primary key column does not have a value then this is an insert.
-   if (!array_key_exists($pkey, $values) or !$values[$pkey] or !isset($values[$pkey])) {
-     // Before inserting, we want to make sure the record does not
-     // already exist.  Using the unique constraint check for a matching record.
-     $options = array('is_duplicate' => TRUE);
-     $is_duplicate = chado_select_record($table_name, array('*'), $values, $options);
-     if($is_duplicate) {
-       $record = chado_select_record($table_name, array('*'), $values);
-       return $record[0]->$pkey;
-     }
+  // If the primary key column has a value but all other values are empty then
+  // this is a delete.
+  if (array_key_exists($pkey, $values) and $values[$pkey]) {
+    $num_vals = 0;
+    foreach ($values as $value) {
+      if ($value) {
+        $num_vals++;
+      }
+    }
+    if ($num_vals == 1) {
+      $new_vals[$pkey] = $values[$pkey];
+      if (!chado_delete_record($table_name, $new_vals)) {
+        throw new Exception('Could not delete record from table: "' . $table_name . '".');
+      }
+      return '';
+    }
+  }
 
 
-     // Insert the values array as a new record in the table but remove the
-     // pkey as it should be set.
-     $new_vals = $values;
-     unset($new_vals[$pkey]);
-     $record = chado_insert_record($table_name, $new_vals);
-     if ($record === FALSE) {
-       throw new Exception('Could not insert Chado record into table: "' . $table_name . '".');
-     }
-     return $record[$pkey];
-   }
+  // If the primary key column does not have a value then this is an insert.
+  if (!array_key_exists($pkey, $values) or !$values[$pkey] or !isset($values[$pkey])) {
+    // Before inserting, we want to make sure the record does not
+    // already exist.  Using the unique constraint check for a matching record.
+    $options = array('is_duplicate' => TRUE);
+    $is_duplicate = chado_select_record($table_name, array('*'), $values, $options);
+    if($is_duplicate) {
+      $record = chado_select_record($table_name, array('*'), $values);
+      return $record[0]->$pkey;
+    }
 
 
-   // If we've made it to this point then this is an update.
-   // TODO: what if the unique constraint matches another record?  That is
-   // not being tested for here.
-   $match[$pkey] = $values[$pkey];
-   if (!chado_update_record($table_name, $match, $values)) {
-     drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
-   }
-   return $values[$pkey];
+    // Insert the values array as a new record in the table but remove the
+    // pkey as it should be set.
+    $new_vals = $values;
+    unset($new_vals[$pkey]);
+    $record = chado_insert_record($table_name, $new_vals);
+    if ($record === FALSE) {
+      throw new Exception('Could not insert Chado record into table: "' . $table_name . '".');
+    }
+    return $record[$pkey];
+  }
+
+  // If we've made it to this point then this is an update.
+  // TODO: what if the unique constraint matches another record?  That is
+  // not being tested for here.
+  $match[$pkey] = $values[$pkey];
+  if (!chado_update_record($table_name, $match, $values)) {
+    drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
+  }
+  return $values[$pkey];
 }
 }
 
 
 /**
 /**
@@ -331,7 +310,8 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
  * Merges the values of all fields into a single array keyed by table name.
  * Merges the values of all fields into a single array keyed by table name.
  */
  */
 function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity) {
 function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity) {
-  $new_fields = array();
+  $all_fields = array();
+  $base_fields = array();
 
 
   // Iterate through all of the fields and organize them into a
   // Iterate through all of the fields and organize them into a
   // new fields array keyed by the table name
   // new fields array keyed by the table name
@@ -348,12 +328,12 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
     }
     }
     $chado_table = $field['settings']['chado_table'];
     $chado_table = $field['settings']['chado_table'];
     $chado_column = $field['settings']['chado_column'];
     $chado_column = $field['settings']['chado_column'];
+    $base_table = $field['settings']['base_table'];
 
 
     // Iterate through the field's items. Fields with cardinality ($delta) > 1
     // Iterate through the field's items. Fields with cardinality ($delta) > 1
     // are multi-valued.
     // are multi-valued.
     $items = field_get_items($entity_type, $entity, $field_name);
     $items = field_get_items($entity_type, $entity, $field_name);
     $temp = array();
     $temp = array();
-
     foreach ($items as $delta => $item) {
     foreach ($items as $delta => $item) {
 
 
       // A field may have multiple items. The field can use items
       // A field may have multiple items. The field can use items
@@ -364,30 +344,45 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
         if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
         if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
           $table_name = $matches[1];
           $table_name = $matches[1];
           $column_name = $matches[2];
           $column_name = $matches[2];
-          $temp[$table_name][$delta][$column_name] = $value;
+          // If this field belongs to the base table then we just add
+          // those values in... there's no delta.
+          if ($table_name == $base_table) {
+            $base_fields[$table_name][$column_name] = $value;
+          }
+          else {
+            $temp[$table_name][$delta][$column_name] = $value;
+          }
         }
         }
       }
       }
       // If there is no value set for the field using the
       // If there is no value set for the field using the
-      // chado-[table_name]__[field name] naming schema then check if a 'value' item
-      // is present and if so use that.
+      // chado-[table_name]__[field name] naming schema then check if a 'value'
+      // item is present and if so use that for the table column value.
       if ((!array_key_exists($chado_table, $temp) or
       if ((!array_key_exists($chado_table, $temp) or
            !array_key_exists($delta, $temp[$chado_table]) or
            !array_key_exists($delta, $temp[$chado_table]) or
            !array_key_exists($chado_column, $temp[$chado_table][$delta])) and
            !array_key_exists($chado_column, $temp[$chado_table][$delta])) and
           array_key_exists('value', $items[$delta]) and
           array_key_exists('value', $items[$delta]) and
           !is_array($items[$delta]['value'])) {
           !is_array($items[$delta]['value'])) {
-        $temp[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
+        // If this field belongs to the base table then we just add
+        // those values in... there's no delta.
+        if ($base_table == $chado_table) {
+          $base_fields[$chado_table][$chado_column] = $items[$delta]['value'];
+        }
+        else {
+          $temp[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
+        }
       }
       }
     }
     }
 
 
     // Now merge the records for this field with the $new_fields array
     // Now merge the records for this field with the $new_fields array
     foreach ($temp as $table_name => $details) {
     foreach ($temp as $table_name => $details) {
       foreach ($details as $delta => $list) {
       foreach ($details as $delta => $list) {
-        $new_fields[$table_name][] = $list;
+        $all_fields[$table_name][] = $list;
       }
       }
     }
     }
   }
   }
 
 
-  return $new_fields;
+  $all_fields = array_merge($base_fields, $all_fields);
+  return $all_fields;
 }
 }
 
 
 /**
 /**

+ 53 - 47
tripal_chado/includes/tripal_chado.fields.inc

@@ -134,6 +134,7 @@ function tripal_chado_bundle_create_fields_base(&$info, $details, $entity_type,
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => $column_name,
         'chado_column' => $column_name,
+        'base_table' => $table_name,
         'semantic_web' => tripal_get_chado_semweb_term($table_name, $column_name),
         'semantic_web' => tripal_get_chado_semweb_term($table_name, $column_name),
       ),
       ),
     );
     );
@@ -234,6 +235,7 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => 'organism_id',
         'chado_column' => 'organism_id',
+        'base_table' => $table_name,
       ),
       ),
     );
     );
   }
   }
@@ -253,6 +255,7 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => 'dbxref_id',
         'chado_column' => 'dbxref_id',
+        'base_table' => $table_name,
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'dbxref_id'),
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'dbxref_id'),
       ),
       ),
     );
     );
@@ -273,6 +276,7 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => 'md5checksum',
         'chado_column' => 'md5checksum',
+        'base_table' => $table_name,
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'md5checksum'),
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'md5checksum'),
       ),
       ),
     );
     );
@@ -293,11 +297,12 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => 'residues',
         'chado_column' => 'residues',
+        'base_table' => $table_name,
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'residues'),
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'residues'),
       ),
       ),
     );
     );
   }
   }
-  
+
   // FEATURE SEQLEN
   // FEATURE SEQLEN
   if ($table_name == 'feature') {
   if ($table_name == 'feature') {
     $field_name = 'data__sequence_length';
     $field_name = 'data__sequence_length';
@@ -313,6 +318,7 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'settings' => array(
       'settings' => array(
         'chado_table' => $table_name,
         'chado_table' => $table_name,
         'chado_column' => 'seqlen',
         'chado_column' => 'seqlen',
+        'base_table' => $table_name,
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'seqlen'),
         'semantic_web' => tripal_get_chado_semweb_term($table_name, 'seqlen'),
       ),
       ),
     );
     );
@@ -341,24 +347,24 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
   }
   }
 
 
   // ORGANISM TYPE_ID
   // ORGANISM TYPE_ID
-  if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
-    $field_name = 'taxarank__infraspecific_taxon';
-    $field_type = 'taxarank__infraspecific_taxon';
-    $info[$field_name] = array(
-      'field_name' => $field_name,
-      'type' => $field_type,
-      'cardinality' => 1,
-      'locked' => FALSE,
-      'storage' => array(
-        'type' => 'field_chado_storage',
-      ),
-      'settings' => array(
-        'chado_table' => 'organism',
-        'chado_column' => 'type_id',
-        'semantic_web' => 'TAXRANK:0000046',
-      ),
-    );
-  }
+//   if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
+//     $field_name = 'taxarank__infraspecific_taxon';
+//     $field_type = 'taxarank__infraspecific_taxon';
+//     $info[$field_name] = array(
+//       'field_name' => $field_name,
+//       'type' => $field_type,
+//       'cardinality' => 1,
+//       'locked' => FALSE,
+//       'storage' => array(
+//         'type' => 'field_chado_storage',
+//       ),
+//       'settings' => array(
+//         'chado_table' => 'organism',
+//         'chado_column' => 'type_id',
+//         'semantic_web' => 'TAXRANK:0000046',
+//       ),
+//     );
+//   }
 }
 }
 
 
 /**
 /**
@@ -1076,33 +1082,33 @@ function tripal_chado_bundle_create_instances_custom(&$info, $entity_type, $bund
   }
   }
 
 
   // ORGANISM TYPE_ID
   // ORGANISM TYPE_ID
-  if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
-    $field_name = 'taxarank__infraspecific_taxon';
-    $info[$field_name] = array(
-      'field_name' => $field_name,
-      'entity_type' => $entity_type,
-      'bundle' => $bundle->name,
-      'label' => 'Infraspecific Taxon',
-      'description' => 'The Infraspecific Taxon.',
-      'required' => FALSE,
-      'settings' => array(
-        'auto_attach' => TRUE,
-      ),
-      'widget' => array(
-        'type' => 'taxarank__infraspecific_taxon_widget',
-        'settings' => array(
-          'display_label' => 1,
-        ),
-      ),
-      'display' => array(
-        'default' => array(
-          'label' => 'inline',
-          'type' => 'taxarank__infraspecific_taxon_formatter',
-          'settings' => array(),
-        ),
-      ),
-    );
-  }
+//   if ($table_name == 'organism' and array_key_exists('type_id', $schema['fields'])) {
+//     $field_name = 'taxarank__infraspecific_taxon';
+//     $info[$field_name] = array(
+//       'field_name' => $field_name,
+//       'entity_type' => $entity_type,
+//       'bundle' => $bundle->name,
+//       'label' => 'Infraspecific Taxon',
+//       'description' => 'The Infraspecific Taxon.',
+//       'required' => FALSE,
+//       'settings' => array(
+//         'auto_attach' => TRUE,
+//       ),
+//       'widget' => array(
+//         'type' => 'taxarank__infraspecific_taxon_widget',
+//         'settings' => array(
+//           'display_label' => 1,
+//         ),
+//       ),
+//       'display' => array(
+//         'default' => array(
+//           'label' => 'inline',
+//           'type' => 'taxarank__infraspecific_taxon_formatter',
+//           'settings' => array(),
+//         ),
+//       ),
+//     );
+//   }
 }
 }
 
 
 /**
 /**
@@ -1148,7 +1154,7 @@ function tripal_chado_bundle_create_instances_linker(&$info, $entity_type, $bund
       ),
       ),
     );
     );
   }
   }
-  
+
   // CVTERM
   // CVTERM
   $cvterm_table = $table_name . '_cvterm';
   $cvterm_table = $table_name . '_cvterm';
   if (chado_table_exists($cvterm_table)) {
   if (chado_table_exists($cvterm_table)) {