Browse Source

Rearranged relationship field code to make it more simple... still needs work

Stephen Ficklin 7 years ago
parent
commit
f7a993232f

+ 1 - 1
tripal/includes/TripalEntityUIController.inc

@@ -648,7 +648,7 @@ function tripal_entity_form_ajax_callback($form, $form_state) {
    $langcode = 'und';
    foreach ($instances as $field_name => $instance) {
      $entity_type = $instance['entity_type'];
-     if($entity_type == 'TripalEntity') {
+     if($entity_type == 'TripalEntity' and array_key_exists($field_name, $form)) {
        foreach ($form[$field_name][$langcode] as $delta => $field_form) {
          if (!preg_match('/^\d+$/', $delta)) {
            continue;

+ 30 - 0
tripal/tripal.module

@@ -640,6 +640,36 @@ function tripal_form_alter(&$form, $form_state, $form_id) {
   if ($form_id == 'field_ui_field_edit_form' and $form['#instance']['entity_type'] == 'TripalEntity') {
     tripal_field_instance_settings_form_alter($form, $form_state);
   }
+
+  // Remove fields that have no form. It's just a bit too confusing to have
+  // widgets appear in the form but without and form elements inside them.
+  if ($form_id == 'tripal_entity_form') {
+    $children = element_children($form);
+    foreach ($children as $child) {
+      // count the number of form elements
+      if (array_key_exists('und', $form[$child])) {
+        $total_widgets = 0;
+        foreach ($form[$child]['und'] as $delta => $element) {
+          if (is_numeric($delta)) {
+            $total_widgets += count(element_children($element));
+            // Ignore the weight column
+            if (array_key_exists('_weight', $element)) {
+              $total_widgets--;
+            }
+            // Ignore a hidden value column
+            if (array_key_exists('value', $element) and $element['value']['#type'] == 'value') {
+              $total_widgets--;
+            }
+          }
+        }
+        // If we have no widgets then here's not a form for this field so just
+        // remove it.
+        if ($total_widgets == 0) {
+          //unset($form[$child]);
+        }
+      }
+    }
+  }
 }
 
 function tripal_check_new_fields($bundle_name) {

+ 191 - 265
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

@@ -150,29 +150,28 @@ class sbo__relationship extends ChadoField {
       )
     );
   }
-  /**
-   *
-   * @see TripalField::load()
-   */
-  public function load($entity) {
-    $settings = $this->field['settings'];
 
-    $record = $entity->chado_record;
-    $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+  private function loadRelationship($relationship, &$entity, $delta) {
 
     $field_name = $this->field['field_name'];
-    $field_type = $this->field['type'];
     $field_table = $this->instance['settings']['chado_table'];
-    $field_column = $this->instance['settings']['chado_column'];
     $base_table = $this->instance['settings']['base_table'];
 
-    // Get the PKey for this table
-    $schema = chado_get_schema($field_table);
-    $pkey = $schema['primary key'][0];
+    $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
+    $rel_type = $relationship->type_id->name;
+    $verb = $this->get_rel_verb($rel_type);
 
     // Get the foreign keys for the subject and object tables
     $subject_fkey_table = '';
     $object_fkey_table = '';
+
+    $schema = chado_get_schema($field_table);
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    // Not all tables have the columns named 'subject_id' and 'object_id'.
+    // some have variations on that name and we need to determine what they are.
     $fkeys = $schema['foreign keys'];
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
@@ -188,37 +187,186 @@ class sbo__relationship extends ChadoField {
         }
       }
     }
+
+    // Get the schemas for the subject and object table.  These should
+    // be the same as the base table but just to be safe we'll get them
+    // separately.
     $subject_schema = chado_get_schema($subject_fkey_table);
-    $object_schema = chado_get_schema($object_fkey_table);
     $subject_pkey = $subject_schema['primary key'][0];
+    $object_schema = chado_get_schema($object_fkey_table);
     $object_pkey = $object_schema['primary key'][0];
 
-    // Get the FK that links to the base record.
+    // Not all realtionshp tables have a name field (e.g. organism_relationship)
+    // threfore in some cases we need to dig a bit deeper to get the entity
+    // name and the entity type name.
+    $subject_name = '';
+    $subject_type = '';
+    $object_name = '';
+    $object_type = '';
+
+    // The linked to table of a relationship linker table may not always
+    // have a type_id or name field.  So we have to be a bit more
+    // specific about how we set some variables.
+    switch ($relationship->tablename) {
+      case 'acquisition_relationship':
+        $subject_type = 'acquisition';
+        $object_type = 'acquisition';
+        break;
+      case 'analysis_relationship':
+        $subject_type = 'analysis';
+        $object_type = 'analysis';
+        break;
+      case 'biomaterial_relationship':
+        $subject_type = 'biomaterial';
+        $object_type = 'biomaterial';
+        break;
+      case 'cell_line_relationship':
+        $subject_type = 'cell_line';
+        $object_type = 'cell_line';
+        break;
+      case 'element_relationship':
+        $subject_name = $relationship->$subject_id_key->feature_id->name;
+        $object_name = $relationship->$object_id_key->feature_id->name;
+        break;
+      case 'organism_relationship':
+        $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
+        $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
+        $subject_type = 'organism';
+        $object_type = 'organism';
+        break;
+      case 'project_relationship':
+        $subject_type = 'project';
+        $object_type = 'project';
+        break;
+      case 'phylonode_relationship':
+        $subject_name = $relationship->$subject_id_key->label;
+        $object_name = $relationship->$object_id_key->label;
+        break;
+      case 'pub_relationship':
+        $subject_name = $relationship->$subject_id_key->uniquename;
+        $object_name = $relationship->$object_id_key->uniquename;
+        break;
+      case 'quantification_relationship':
+        $subject_type = 'quantification';
+        $object_type = 'quantification';
+        break;
+      default:
+        $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
+        $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
+        $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
+        $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
+    }
+
+    $entity->{$field_name}['und'][$delta]['value'] = array(
+      'local:relationship_subject' => array(
+        'rdfs:type' => $subject_type,
+        'schema:name' => $subject_name,
+      ),
+      'local:relationship_type' => $relationship->type_id->name,
+      'local:relationship_object' => array(
+        'rdfs:type' => $object_type,
+        'schema:name' => $object_name,
+        'entity' => 'TripalEntity:' . $entity->id,
+      )
+    );
+
+    // Get the primary key for the subject and object.
+//     $subject_id = $relationship->$subject_id_key->$subject_pkey;
+//     $sentity_id = chado_get_record_entity_by_table($subject_fkey_table, $subject_id);
+//     if ($sentity_id) {
+//       $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $sentity_id;
+//     }
+
+//     // See if an entity exists for the object.
+//     $object_id = $relationship->$object_id_key->$object_pkey;
+//     $sentity_id = chado_get_record_entity_by_table($object_fkey_table, $object_id);
+//     if ($sentity_id) {
+//       $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $sentity_id;
+//     }
+
+    // If the subject or object have a unqiuename then add that in for refernce.
+    if (property_exists($relationship->$subject_id_key, 'uniquename')) {
+      $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;;
+    }
+    if (property_exists($relationship->$object_id_key, 'uniquename')) {
+      $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
+    }
+
+    // Add in the TripalEntity ids if these base records in the relationship
+    // are published.
+    if (property_exists($relationship->$subject_id_key, 'entity_id')) {
+      $entity_id = $relationship->$subject_id_key->entity_id;
+      $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $entity_id;
+    }
+    if (property_exists($relationship->$object_id_key, 'entity_id')) {
+      $entity_id = $relationship->$object_id_key->entity_id;
+      $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $entity_id;
+    }
+
+    // Add the clause to the values array.  The clause is a written version
+    // of the relationships.
+    $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
+    $entity->{$field_name}['und'][$delta]['value']['SIO:000493'] = 'The ' . $subject_type . ', ' .
+      $subject_name . ', ' . $verb . ' '  . $rel_type_clean . ' this '  .
+      $object_type . '.';
+
+    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
+    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$subject_pkey;
+    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
+    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $object_id_key] = $relationship->$object_id_key->$object_pkey;
+
+    // For the widget to work properly we will preform values.
+    $entity->{$field_name}['und'][$delta]['type_name'] = $relationship->type_id->name;
+    $entity->{$field_name}['und'][$delta]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$subject_pkey . ']';
+    $entity->{$field_name}['und'][$delta]['object_name'] = $object_name  . ' [id: ' . $relationship->$object_id_key->$object_pkey . ']';
+    if (array_key_exists('value', $schema['fields'])) {
+      $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__value'] = $relationship->value;
+    }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__rank'] = $relationship->rank;
+    }
+  }
+ /**
+   *
+   * @see TripalField::load()
+   */
+  public function load($entity) {
+    $settings = $this->field['settings'];
+
+    $record = $entity->chado_record;
+    $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+    $base_table = $this->instance['settings']['base_table'];
+
+    // Get the PKey for this table
     $schema = chado_get_schema($field_table);
+    $pkey = $schema['primary key'][0];
     $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
     $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
 
+    // Not all tables have the columns named 'subject_id' and 'object_id'.
+    // some have variations on that name and we need to determine what they are.
+    $fkeys = $schema['foreign keys'];
+    $subject_id_key = 'subject_id';
+    $object_id_key = 'object_id';
+    foreach ($fkeys as $fktable => $details) {
+      foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
+        if (preg_match('/^subject_.*id/', $fkey_lcolumn)) {
+          $subject_id_key = $fkey_lcolumn;
+        }
+        if (preg_match('/^object_.*id/', $fkey_lcolumn)) {
+          $object_id_key = $fkey_lcolumn;
+        }
+      }
+    }
+
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
-      'value' => array(
-      /* The following shows what may be present in the value array
-        // Clause
-        'SIO:000493' => '',
-        'local:relationship_subject' => array(
-          // Identifier
-          'data:0842' => '',
-          'schema:name' => '',
-          'rdfs:type' => ''
-        ),
-        'local:relationship_object' => array(
-          // Identifier
-          'data:0842' => '',
-          'schema:name' => '',
-          'rdfs:type' => '',
-        ),
-        'local:relationship_type' => '',
-      */
-      ),
+      'value' => '',
       'chado-' . $field_table . '__' . $pkey => '',
       'chado-' . $field_table . '__' . $subject_id_key => '',
       'chado-' . $field_table . '__' . $object_id_key => '',
@@ -269,244 +417,22 @@ class sbo__relationship extends ChadoField {
     if (!$record->$rel_table) {
       return;
     }
-    $srelationships = null;
-    $orelationships = null;
 
+    // Load the subject relationships
+    $i = 0;
     if (isset($record->$rel_table->$subject_id_key)) {
       $srelationships = $record->$rel_table->$subject_id_key;
-    }
-    if (isset($record->$rel_table->$object_id_key)) {
-      $orelationships = $record->$rel_table->$object_id_key;
-    }
-
-    $i = 0;
-    if ($orelationships) {
-      foreach ($orelationships as $relationship) {
-        $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
-        $rel_type = $relationship->type_id->name;
-        $verb = $this->get_rel_verb($rel_type);
-        $subject_id = $relationship->$subject_id_key->$subject_pkey;
-
-        // The linked to table of a relationship linker table may not always
-        // have a type_id or name field.  So we have to be a bit more
-        // specific about how we set some variables.
-        switch ($relationship->tablename) {
-          case 'acquisition_relationship':
-            $subject_type = 'acquisition';
-            $object_type = 'acquisition';
-            break;
-          case 'analysis_relationship':
-            $subject_type = 'analysis';
-            $object_type = 'analysis';
-            break;
-          case 'biomaterial_relationship':
-            $subject_type = 'biomaterial';
-            $object_type = 'biomaterial';
-            break;
-          case 'cell_line_relationship':
-            $subject_type = 'cell_line';
-            $object_type = 'cell_line';
-            break;
-          case 'element_relationship':
-            $subject_name = $relationship->$subject_id_key->feature_id->name;
-            $object_name = $relationship->$object_id_key->feature_id->name;
-            break;
-          case 'organism_relationship':
-            $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
-            $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
-            $subject_type = 'organism';
-            $object_type = 'organism';
-            break;
-          case 'project_relationship':
-            $subject_type = 'project';
-            $object_type = 'project';
-            break;
-          case 'phylonode_relationship':
-            $subject_name = $relationship->$subject_id_key->label;
-            $object_name = $relationship->$object_id_key->label;
-            break;
-          case 'pub_relationship':
-            $subject_name = $relationship->$subject_id_key->uniquename;
-            $object_name = $relationship->$object_id_key->uniquename;
-            break;
-          case 'quantification_relationship':
-            $subject_type = 'quantification';
-            $object_type = 'quantification';
-            break;
-          default:
-            $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
-            $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
-            $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
-            $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
-        }
-
-        $entity->{$field_name}['und'][$i]['value'] = array(
-          'local:relationship_subject' => array(
-            'rdfs:type' => $subject_type,
-            'schema:name' => $subject_name,
-          ),
-          'local:relationship_type' => $relationship->type_id->name,
-          'local:relationship_object' => array(
-            'rdfs:type' => $object_type,
-            'schema:name' => $object_name,
-            'entity' => 'TripalEntity:' . $entity->id,
-          )
-        );
-
-        // See if an entity exists for the subject.
-        $data_table = preg_replace('/_relationship/', '', $relationship->tablename);
-        $sentity_id = chado_get_record_entity_by_table($data_table, $subject_id);
-        if ($sentity_id) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $sentity_id;
-        }
-
-        if (property_exists($relationship->$subject_id_key, 'uniquename')) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['data:0842'] =  $relationship->$subject_id_key->uniquename;;
-        }
-        if (property_exists($relationship->$object_id_key, 'uniquename')) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
-        }
-        if (property_exists($relationship->$subject_id_key, 'entity_id')) {
-          $entity_id = $relationship->$subject_id_key->entity_id;
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $entity_id;
-        }
-
-        // Add the clause to the values array.
-        $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
-        $entity->{$field_name}['und'][$i]['value']['SIO:000493'] = 'The ' . $subject_type . ', ' .
-          $subject_name . ', ' . $verb . ' '  . $rel_type_clean . ' this '  .
-          $object_type . '.';
-
-        $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
-        $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$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_key] = $relationship->$object_id_key->$object_pkey;
-
-        $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
-        $entity->{$field_name}['und'][$i]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
-        $entity->{$field_name}['und'][$i]['object_name'] = $object_name  . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
-        if (array_key_exists('value', $schema['fields'])) {
-          $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
-        }
-        if (array_key_exists('rank', $schema['fields'])) {
-          $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
-        }
+      foreach ($srelationships as $relationship) {
+        $this->loadRelationship($relationship, $entity, $i);
         $i++;
       }
     }
 
-    if ($srelationships) {
-      foreach ($srelationships as $relationship) {
-        $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
-        $rel_type = $relationship->type_id->name;
-        $verb = $this->get_rel_verb($rel_type);
-        $object_id = $relationship->$object_id_key->$object_pkey;
-
-        // The linked to table of a relationship linker table may not always
-        // have a type_id or name field.  So we have to be a bit more
-        // specific about how we set some variables.
-        switch ($relationship->tablename) {
-          case 'acquisition_relationship':
-            $subject_type = 'acquisition';
-            $object_type = 'acquisition';
-            break;
-          case 'analysis_relationship':
-            $subject_type = 'analysis';
-            $object_type = 'analysis';
-            break;
-          case 'biomaterial_relationship':
-            $subject_type = 'biomaterial';
-            $object_type = 'biomaterial';
-            break;
-          case 'cell_line_relationship':
-            $subject_type = 'cell_line';
-            $object_type = 'cell_line';
-            break;
-          case 'element_relationship':
-            $subject_name = $relationship->$subject_id_key->feature_id->name;
-            $object_name = $relationship->$object_id_key->feature_id->name;
-            break;
-          case 'organism_relationship':
-            $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
-            $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
-            $subject_type = 'organism';
-            $object_type = 'organism';
-            break;
-          case 'project_relationship':
-            $subject_type = 'project';
-            $object_type = 'project';
-            break;
-          case 'phylonode_relationship':
-            $subject_name = $relationship->$subject_id_key->label;
-            $object_name = $relationship->$object_id_key->label;
-            break;
-          case 'pub_relationship':
-            $subject_name = $relationship->$subject_id_key->uniquename;
-            $object_name = $relationship->$object_id_key->uniquename;
-            break;
-          case 'quantification_relationship':
-            $subject_type = 'quantification';
-            $object_type = 'quantification';
-            break;
-          default:
-            $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
-            $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
-            $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
-            $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
-        }
-
-        $entity->{$field_name}['und'][$i]['value'] = array(
-          'local:relationship_subject' => array(
-            'rdfs:type' => $subject_type,
-            'schema:name' => $subject_name,
-            'entity' => 'TripalEntity:' . $entity->id,
-          ),
-          'local:relationship_type' => $relationship->type_id->name,
-          'local:relationship_object' => array(
-            'rdfs:type' =>  $object_type,
-            'schema:name' => $object_name,
-          )
-        );
-
-        // See if an entity exists for the object.
-        $data_table = preg_replace('/_relationship/', '', $relationship->tablename);
-        $oentity_id = chado_get_record_entity_by_table($data_table, $object_id);
-        if ($oentity_id) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $oentity_id;
-        }
-
-        if (property_exists($relationship->$subject_id_key, 'uniquename')) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;
-        }
-        if (property_exists($relationship->$object_id_key, 'uniquename')) {
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
-        }
-        if (property_exists($relationship->$object_id_key, 'entity_id')) {
-          $entity_id = $relationship->$object_id_key->entity_id;
-          $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $entity_id;
-        }
-
-        // Add the clause to the value array.
-        $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
-        $entity->{$field_name}['und'][$i]['value']['SIO:000493'] = 'This ' .
-          $subject_type . ' ' . $verb . ' '  . $rel_type_clean . ' the '  .
-          $object_type . ' ' . $object_name . '.';
-
-        $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
-        $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$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_key] = $relationship->$object_id_key->$object_pkey;
-
-        $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
-        $entity->{$field_name}['und'][$i]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
-        $entity->{$field_name}['und'][$i]['object_name'] = $object_name . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
-
-        if (array_key_exists('value', $schema['fields'])) {
-          $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
-        }
-        if (array_key_exists('rank', $schema['fields'])) {
-          $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
-        }
+    // Load the object relationships
+    if (isset($record->$rel_table->$object_id_key)) {
+      $orelationships = $record->$rel_table->$object_id_key;
+      foreach ($orelationships as $relationship) {
+        $this->loadRelationship($relationship, $entity, $i);
         $i++;
       }
     }

+ 8 - 3
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc

@@ -14,6 +14,10 @@ class sbo__relationship_widget extends ChadoFieldWidget {
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
 
+    // TODO: make this widget deal better with the various relationship
+    // tables. See the load function as it does a better job of this.
+    return;
+
     // Get the field settings.
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
@@ -329,15 +333,16 @@ class sbo__relationship_widget extends ChadoFieldWidget {
 
   /**
    *
-   * @see TripalFieldWidget::submit()
+   * @see TripalFieldWidget::validate()
    */
-  public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
+  public function validate($element, $form, &$form_state, $langcode, $delta) {
+
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $field_table = $this->instance['settings']['chado_table'];
     $field_column = $this->instance['settings']['chado_column'];
     $base_table = $this->instance['settings']['base_table'];
-    $chado_record_id = $entity->chado_record_id;
+    $chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
 
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];

+ 0 - 1
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -43,7 +43,6 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
 
   // Convert the fields into a key/value list of fields and their values.
   $field_vals = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
-  //dpm($field_vals);
 
   // First, write the record for the base table.  If we have a record id then
   // this is an update and we need to set the primary key.  If not, then this