Răsfoiți Sursa

Fixes to fields

Stephen Ficklin 7 ani în urmă
părinte
comite
befdb4fc45

+ 1 - 0
tripal_chado/includes/TripalFields/data__accession/data__accession.inc

@@ -83,6 +83,7 @@ class data__accession extends ChadoField {
   public function query($query, $condition) {
     $alias = $this->field['field_name'];
     $operator = $condition['operator'];
+
     $field_table = $this->instance['settings']['chado_table'];
     $field_column = $this->instance['settings']['chado_column'];
 

+ 39 - 8
tripal_chado/includes/TripalFields/local__source_data/local__source_data.inc

@@ -73,26 +73,20 @@ class local__source_data extends ChadoField {
         'elements' => array(
           $sourcename_term => array(
             'searchable' => TRUE,
-            'name' => 'sourcename',
             'label' => 'Data Source Name',
             'help' => 'The name of the data source used for the analysis.',
-            'operations' => array('eq', 'ne', 'contains', 'starts'),
             'sortable' => TRUE,
           ),
           $sourceversion_term => array(
-            'searchable' => FALSE,
-            'name' => 'sourceversion',
+            'searchable' => TRUE,
             'label' => 'Data Source Version',
             'help' => 'If applicable, the version number of the source data used for the analysis.',
-            'operations' => array('eq', 'ne', 'contains', 'starts'),
-            'sortable' => FALSE,
+            'sortable' => TRUE,
           ),
           $sourceuri_term => array(
             'searchable' => FALSE,
-            'name' => 'sourceuri',
             'label' => 'Data Source URI',
             'help' => 'If applicable, the universal resource indicator (e.g. URL) of the source data used for the analysis.',
-            'operations' => array(),
             'sortable' => FALSE,
           ),
         ),
@@ -100,6 +94,43 @@ class local__source_data extends ChadoField {
     );
   }
 
+  /**
+   * @see ChadoField::query()
+   */
+  public function query($query, $condition) {
+    $operator = $condition['operator'];
+
+    $field_term_id = $this->getFieldTermID();
+    $sourcename_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourcename');
+    $sourceversion_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourceversion');
+    $sourceuri_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourceuri');
+
+    if ($condition['column'] == $sourcename_term) {
+      $query->condition("base.sourcename", $condition['value'], $operator);
+    }
+    if ($condition['column'] == $sourceversion_term) {
+      $query->condition("base.sourceversion", $condition['value'], $operator);
+    }
+  }
+  /**
+   * @see ChadoField::queryOrder()
+   */
+  public function queryOrder($query, $order) {
+    $field_term_id = $this->getFieldTermID();
+    $sourcename_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourcename');
+    $sourceversion_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourceversion');
+    $sourceuri_term = $field_term_id . ',' . tripal_get_chado_semweb_term('analysis', 'sourceuri');
+
+    dpm($order);
+    dpm($sourceversion_term);
+    if ($order['column'] == $sourcename_term) {
+      $query->orderBy("base.sourcename", $order['direction']);
+    }
+    if ($order['column'] == $sourceversion_term) {
+      $query->orderBy("base.sourceversion", $order['direction']);
+    }
+  }
+
   /**
    *
    * @see TripalField::load()

+ 7 - 3
tripal_chado/includes/TripalFields/local__source_data/local__source_data_formatter.inc

@@ -13,16 +13,20 @@ class local__source_data_formatter extends ChadoFieldFormatter {
    */
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
 
+    $sourcename_term = tripal_get_chado_semweb_term('analysis', 'sourcename');
+    $sourceversion_term = tripal_get_chado_semweb_term('analysis', 'sourceversion');
+    $sourceuri_term = tripal_get_chado_semweb_term('analysis', 'sourceuri');
+
     $content = 'The data source is not provided.';
     if ($items[0]['value']) {
       $content = "<dl class=\"tripal-dl\">";
-      if (!empty($items[0]['value']['schema:name'])) {
+      if (!empty($items[0]['value'][$sourcename_term])) {
         $content .= "<dt>Source Name</dt><dd>: " . $items[0]['value']['schema:name'] . " </dd>";
       }
-      if (!empty($items[0]['value']['IAO:0000129'])) {
+      if (!empty($items[0]['value'][$sourceversion_term])) {
         $content .= "<dt>Source Version</dt><dd>: " . $items[0]['value']['IAO:0000129'] . " </dd>";
       }
-      if (!empty($items[0]['value']['data:1047'])) {
+      if (!empty($items[0]['value'][$sourceuri_term])) {
         $content .= "<dt>Source URI</dt><dd>: " . l($items[0]['value']['data:1047'], $items[0]['value']['data:1047'], array('attributes' => array('target' => '_blank'))) . " </dd>";
       }
       $content .= "</dl>";

+ 40 - 15
tripal_chado/includes/TripalFields/local__source_data/local__source_data_widget.inc

@@ -36,15 +36,28 @@ class local__source_data_widget extends ChadoFieldWidget {
     // submit).
     if (array_key_exists('values', $form_state) and
         array_key_exists($field_name, $form_state['values'])) {
-      $sourcename = $form_state['values'][$field_name][$langcode][$delta]['chado-analysis__sourcename'];
-      $sourceversion = $form_state['values'][$field_name][$langcode][$delta]['chado-analysis__sourceversion'];
-      $sourceuri = $form_state['values'][$field_name][$langcode][$delta]['chado-analysis__sourceuri'];
+      $sourcename = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourcename'];
+      $sourceversion = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceversion'];
+      $sourceuri = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceuri'];
     }
 
     $widget['value'] = array(
       '#type' => 'value',
       '#value' => $sourcename,
     );
+    $widget['chado-analysis__sourcename'] = array(
+      '#type' => 'value',
+      '#value' => $sourcename,
+    );
+    $widget['chado-analysis__sourceversion'] = array(
+      '#type' => 'value',
+      '#value' => $sourceversion,
+    );
+    $widget['chado-analysis__sourceuri'] = array(
+      '#type' => 'value',
+      '#value' => $sourceuri,
+    );
+
     $widget['source_data'] = array(
       '#type' => 'fieldset',
       '#title' => $this->instance['label'],
@@ -52,30 +65,29 @@ class local__source_data_widget extends ChadoFieldWidget {
       '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
       '#delta' => $delta,
     );
-    $widget['source_data']['chado-analysis__sourcename'] = array(
+    $widget['source_data']['sourcename'] = array(
       '#type' => 'textfield',
       '#title' => 'Data Source Name',
       '#description' => 'The name of the source where data was obtained for this analysis.',
       '#default_value' => $sourcename,
       '#required' => TRUE,
     );
-    $widget['source_data']['chado-analysis__sourceversion'] = array(
+    $widget['source_data']['sourceversion'] = array(
       '#type' => 'textfield',
       '#title' => 'Data Source Version',
       '#description' => 'The version number of the data source (if applicable).',
       '#default_value' => $sourceversion,
     );
-    $widget['source_data']['chado-analysis__sourceuri'] = array(
+    $widget['source_data']['sourceuri'] = array(
       '#type' => 'textfield',
       '#title' => 'Data Source URI',
       '#description' => 'The URI (e.g. web URL) where the source data can be retreived.',
       '#default_value' => $sourceuri,
     );
+
   }
-  /**
-   * @see TripalFieldWidget::submit()
-   */
-  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'];
     $table_name = $this->instance['settings']['chado_table'];
@@ -83,10 +95,23 @@ class local__source_data_widget extends ChadoFieldWidget {
     $field_column = $this->instance['settings']['chado_column'];
     $base_table = $this->instance['settings']['base_table'];
 
-    // Using a fieldset in our widget above throws off the structure
-    // of the values array.  So we need to reorganize it a bit:
-    $value = $form_state['values'][$field_name][$langcode][$delta]['value'];
-    $form_state['values'][$field_name][$langcode][$delta] = $form_state['values'][$field_name][$langcode][$delta]['source_data'];
-    $form_state['values'][$field_name][$langcode][$delta]['value'] = $value;
+    $source_name = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourcename'];
+    $sourceversion = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceversion'];
+    $sourceuri = $form_state['values'][$field_name]['und'][$delta]['source_data']['sourceuri'];
+
+    // Set the value so the form element won't be ignored.
+    $form_state['values'][$field_name]['und'][$delta]['value'] = $source_name;
+
+    if (!$sourceversion) {
+      $sourceversion = '__NULL__';
+    }
+    if (!$sourceuri) {
+      $sourceuri = '__NULL__';
+    }
+
+    // And set the values for our Chado insert/update
+    $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourcename'] = $source_name;
+    $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourceversion'] = $sourceversion;
+    $form_state['values'][$field_name]['und'][$delta]['chado-analysis__sourceuri'] = $sourceuri;
   }
 }

+ 2 - 2
tripal_chado/includes/TripalFields/obi__organism/obi__organism_widget.inc

@@ -53,7 +53,7 @@ class obi__organism_widget extends ChadoFieldWidget {
     $field_table = $this->instance['settings']['chado_table'];
 
     // Make sure the value is set to the organism_id
-    $organism_id = $form_state['values'][$field_name][$langcode][0]['chado-' . $field_table . '__organism_id'];
-    $form_state['values'][$field_name][$langcode][0]['value'] = $organism_id;
+    $organism_id = $form_state['values'][$field_name]['und'][0]['chado-' . $field_table . '__organism_id'];
+    $form_state['values'][$field_name]['und'][0]['value'] = $organism_id;
   }
 }

+ 30 - 39
tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference.inc

@@ -62,10 +62,11 @@ class sbo__database_cross_reference extends ChadoField {
    */
   public function elementInfo() {
 
+    $field_term = $this->getFieldTermID();
     $dbname_term = tripal_get_chado_semweb_term('db', 'name');
     $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
+    $dburl_term = tripal_get_chado_semweb_term('db', 'url');
 
-    $field_term = $this->getFieldTermID();
     return array(
       $field_term => array(
         'operations' => array(),
@@ -74,23 +75,18 @@ class sbo__database_cross_reference extends ChadoField {
         'elements' => array(
           $dbname_term => array(
             'searchable' => TRUE,
-            'name' => 'db_name',
             'label' => 'Database Name',
             'help' => 'The name of the remote database that houses the cross reference.',
-            'operations' => array('eq', 'ne', 'contains', 'starts'),
             'sortable' => TRUE,
           ),
           $accession_term => array(
             'searchable' => TRUE,
-            'name' => 'accession',
             'label' => 'Database Accession',
             'help' => 'The unique accession (identifier) in the database that houses the cross reference.',
-            'operations' => array('eq', 'ne', 'contains', 'starts'),
             'sortable' => TRUE,
           ),
-          'schema:url' => array(
+          $dburl_term => array(
             'searchable' => FALSE,
-            'name' => 'URL',
             'label' => 'Database URL',
             'help' => 'The URL of the database that houses the cross reference.',
             'sortable' => FALSE,
@@ -117,25 +113,18 @@ class sbo__database_cross_reference extends ChadoField {
     $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
     $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
 
+    $dbname_term = tripal_get_chado_semweb_term('db', 'name');
+    $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
+    $dburl_term = tripal_get_chado_semweb_term('db', 'url');
+
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
-      'value' => array(
-        /* sub elements that may be present in the values array
-        // Database name
-        'data:1048' => '',
-        // Accession
-        'data:2091' => '',
-        'schema:url' => '',
-        */
-      ),
+      'value' => '',
       'chado-' . $field_table . '__' . $pkey => '',
-      'chado-' . $field_table . '__' . $fkey_lcolumn => '',
+      'chado-' . $field_table . '__' . $fkey_lcolumn => $record->$fkey_rcolumn,
       'chado-' . $field_table . '__dbxref_id' => '',
-      'dbxref_id' => '',
       'db_id' => '',
       'accession' => '',
-      'version' => '',
-      'description' => '',
     );
 
     $linker_table = $base_table . '_dbxref';
@@ -155,18 +144,15 @@ class sbo__database_cross_reference extends ChadoField {
         $URL = tripal_get_dbxref_url($dbxref);
         $entity->{$field_name}['und'][$i] = array(
           'value' => array(
-            'data:1048' => $dbxref->db_id->name,
-            'data:2091' => $dbxref->accession,
-            'schema:url' => $URL,
+            $dbname_term => $dbxref->db_id->name,
+            $accession_term => $dbxref->accession,
+            $dburl_term => $URL,
           ),
           'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
           'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn->$fkey_lcolumn,
           'chado-' . $field_table . '__dbxref_id' => $dbxref->dbxref_id,
-          'dbxref_id' => $dbxref->dbxref_id,
           'db_id' => $dbxref->db_id->db_id,
           'accession' => $dbxref->accession,
-          'version' => $dbxref->version,
-          'description' => $dbxref->description,
         );
         $i++;
       }
@@ -179,21 +165,28 @@ class sbo__database_cross_reference extends ChadoField {
   public function query($query, $condition) {
     $dbxref_linker = $this->instance['settings']['chado_table'];
     $base_table = $this->instance['settings']['base_table'];
+    $field_table = $this->instance['settings']['chado_table'];
+
     $bschema = chado_get_schema($base_table);
     $bpkey = $bschema['primary key'][0];
-    $alias = 'dbx_linker';
+
+    $alias = $this->field['field_name'];
     $operator = $condition['operator'];
 
-    if ($condition['column'] == 'database_cross_reference.database_name') {
-      $this->queryJoinOnce($query, $dbxref_linker, $alias, "base.$bpkey = $alias.$bpkey");
-      $this->queryJoinOnce($query, 'dbxref', 'DBX', "DBX.dbxref_id = $alias.dbxref_id");
-      $this->queryJoinOnce($query, 'db', 'DB', "DB.db_id = DBX.db_id");
-      $query->condition("DB.name", $condition['value'], $operator);
+    $field_term_id = $this->getFieldTermID();
+    $dbname_term = $field_term_id . ',' . tripal_get_chado_semweb_term('db', 'name');
+    $accession_term = $field_term_id . ',' . tripal_get_chado_semweb_term('dbxref', 'accession');
+    $dburl_term = $field_term_id . ',' . tripal_get_chado_semweb_term('db', 'url');
+
+    $this->queryJoinOnce($query, $field_table, $alias, "base.$bpkey = $alias.$bpkey");
+    $this->queryJoinOnce($query, 'dbxref', $alias . '_DBX', $alias . "_DBX.dbxref_id = $alias.dbxref_id");
+
+    if ($condition['column'] == $dbname_term) {
+      $this->queryJoinOnce($query, $alias . '_DB', $alias . "_DB.db_id = " . $alias . "_DBX.db_id");
+      $query->condition($alias . "_DB.name", $condition['value'], $operator);
     }
-    if ($condition['column'] == 'database_cross_reference.accession') {
-      $this->queryJoinOnce($query, $dbxref_linker, $alias, "base.$bpkey = $alias.$bpkey");
-      $this->queryJoinOnce($query, 'dbxref', 'DBX', "DBX.dbxref_id = $alias.dbxref_id");
-      $query->condition("DBX.accession", $condition['value'], $operator);
+    if ($condition['column'] == $accession_term) {
+      $query->condition($alias . "_DBX.dbxref", $condition['value'], $operator);
     }
   }
 
@@ -228,8 +221,6 @@ class sbo__database_cross_reference extends ChadoField {
       $dbxref_id = $values['chado-' . $field_table . '__dbxref_id'];
       $db_id = $values['db_id'];
       $accession = $values['accession'];
-      $version = $values['version'];
-      $description = $values['description'];
 
       // 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
@@ -250,7 +241,7 @@ class sbo__database_cross_reference extends ChadoField {
           'error' => 'sbo__database_cross_reference',
         );
       }
-      if (!$db_id and !$accession and ($version or $description)) {
+      if (!$db_id and !$accession) {
         $errors[$field_name][$delta]['und'][] = array(
           'message' => t("A database and the accession must both be provided."),
           'error' => 'sbo__database_cross_reference',

+ 10 - 3
tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference_formatter.inc

@@ -14,13 +14,20 @@ class sbo__database_cross_reference_formatter extends ChadoFieldFormatter {
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
     $chado_table = $this->instance['settings']['chado_table'];
     $content = '';
+
+    $dbname_term = tripal_get_chado_semweb_term('db', 'name');
+    $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
+    $dburl_term = tripal_get_chado_semweb_term('db', 'url');
+
     foreach ($items as $delta => $item) {
       if (!$item['value']) {
         continue;
       }
-      $content = $item['value']['data:1048'] . ':' . $item['value']['data:2091'];
-      if ($item['value']['schema:url']) {
-        $content = l($content, $item['value']['schema:url'], array('attributes' => array('target' => '_blank')));
+      $content = $item['value'][$dbname_term] . ':' . $item['value'][$accession_term];
+      if ($item['value'][$dburl_term]) {
+        $dbxref = tripal_get_dbxref(array('dbxref_id' => $item['chado-feature_dbxref__dbxref_id']));
+        $url = tripal_get_dbxref_url($dbxref);
+        $content = l($content, $url, array('attributes' => array('target' => '_blank')));
       }
       $element[$delta] = array(
         '#type' => 'markup',

+ 24 - 120
tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference_widget.inc

@@ -13,15 +13,14 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
    */
   public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
     parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+
     $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'];
-
-    // Get the FK column that links to the base table.
-    $chado_table = $this->instance['settings']['chado_table'];
     $base_table = $this->instance['settings']['base_table'];
-    $schema = chado_get_schema($chado_table);
+
+    $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
@@ -32,8 +31,6 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
     $dbxref_id = '';
     $db_id = '';
     $accession = '';
-    $version = '';
-    $description = '';
 
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
@@ -43,27 +40,17 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
       $dbxref_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__dbxref_id', $dbxref_id);
       $db_id = tripal_get_field_item_keyval($items, $delta, 'db_id', $db_id);
       $accession = tripal_get_field_item_keyval($items, $delta, 'accession', $accession);
-      $version = tripal_get_field_item_keyval($items, $delta, 'version', $version);
-      $description = tripal_get_field_item_keyval($items, $delta, 'description', $description);
     }
 
     // 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'])) {
-      $record_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $pkey];
-      $fkey_value = $form_state['values'][$field_name]['und'][$delta][$field_table . '__' . $fkey];
-      $dbxref_id = $form_state['values'][$field_name]['und'][$delta][$field_table . '__dbxref_id'];
+    if (array_key_exists('values', $form_state)) {
+      $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
+      $fkey_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey];
+      $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
       $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
       $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
-      $version = $form_state['values'][$field_name]['und'][$delta]['version'];
-      $description = $form_state['values'][$field_name]['und'][$delta]['description'];
     }
 
-    $schema = chado_get_schema('dbxref');
-    $options = tripal_get_db_select_options();
-
-    $widget['#table_name'] = $chado_table;
-    $widget['#fkey_field'] = $fkey;
-    //$widget['#element_validate'] = array('sbo__database_cross_reference_widget_validate');
     $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id-$delta'>";
     $widget['#suffix'] =  "</span>";
 
@@ -85,10 +72,7 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
       '#type' => 'value',
       '#default_value' => $dbxref_id,
     );
-    $widget['dbxref_id'] = array(
-      '#type' => 'value',
-      '#default_value' => $dbxref_id,
-    );
+    $options = tripal_get_db_select_options();
     $widget['db_id'] = array(
       '#type' => 'select',
       '#title' => t('Database'),
@@ -102,6 +86,7 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
         'method' => 'replace'
       ),
     );
+    $schema = chado_get_schema('dbxref');
     $widget['accession'] = array(
       '#type' => 'textfield',
       '#title' => t('Accession'),
@@ -110,93 +95,47 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
       '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
       '#size' => 15,
       '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
-      '#ajax' => array(
-        'callback' => "sbo__database_cross_reference_widget_form_ajax_callback",
-        'wrapper' => "$field_name-dbxref--db-id-$delta",
-        'effect' => 'fade',
-        'method' => 'replace'
-      ),
-      '#disabled' => $db_id ? FALSE : TRUE,
-    );
-    $widget['version'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Version'),
-      '#default_value' => $version,
-      '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
-      '#size' => 5,
-      '#disabled' => $db_id ? FALSE : TRUE,
-    );
-    $widget['description'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Description'),
-      '#default_value' => $description,
-      '#size' => 20,
       '#disabled' => $db_id ? FALSE : TRUE,
     );
-    if (!$db_id) {
-      $widget['links'] = array(
-        '#type' => 'item',
-        '#markup' => l('Add a database', 'admin/tripal/loaders/chado_db/add', array('attributes' => array('target' => '_blank')))
-      );
-    }
   }
 
+  public function validate($element, $form, &$form_state, $langcode, $delta) {
 
-  /**
-   *
-   * @see TripalFieldWidget::submit()
-   */
-  public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $table_name = $this->instance['settings']['chado_table'];
     $field_table = $this->instance['settings']['chado_table'];
     $field_column = $this->instance['settings']['chado_column'];
     $base_table = $this->instance['settings']['base_table'];
+
     $schema = chado_get_schema($table_name);
     $pkey = $schema['primary key'][0];
     $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
     $fkey = $fkeys[0];
 
     // 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'] : '';
-    $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'] : '';
-    $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'] : '';
-
+    $dbxref_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'];
+    $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
+    $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
 
+    // If user did not select a database, we want to remove the dbxref record.
+    // We do this by setting all values to empty except the value and the
+    // primary key.
+    if (!$db_id) {
+      $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = '';
+    }
     // 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
     // value accordingly.
     if ($db_id and $accession) {
-      // If the dbxref doesn't exist then let's add it.
-      $values = array(
-        'db_id' => $db_id,
-        'accession' => $accession,
-      );
-      $options = array('is_duplicate' => TRUE);
-      $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
-      if (!$has_duplicate) {
-        tripal_insert_dbxref(array(
-          'db_id' => $db_id,
-          'accession' => $accession,
-        ));
-      }
       $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
       if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
-        $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__dbxref_id'] = $dbxref->dbxref_id;
-        $form_state['values'][$field_name][$langcode][$delta]['dbxref_id'] = $dbxref->dbxref_id;
+        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__dbxref_id'] = $dbxref->dbxref_id;
+        $form_state['values'][$field_name]['und'][$delta]['value'] = $dbxref->dbxref_id;
       }
     }
-    // If the db_id and accession are not set, then remove the linker FK
-    // value to the base table, but leave the primary key so the record
-    // can be deleted.
-    else {
-      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
-      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__dbxref_id'] = '';
-    }
-
   }
 
   /**
@@ -204,11 +143,6 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
    */
   public function theme($element) {
 
-    // These two fields were added to the widget to help identify the fields
-    // for layout.
-    $table_name = $element['#table_name'];
-    $fkey = $element['#fkey_field'];
-
     $layout = "
       <div class=\"secondary-dbxref-widget\">
         <div class=\"secondary-dbxref-widget-item\">" .
@@ -217,13 +151,6 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
         <div class=\"secondary-dbxref-widget-item\">" .
           drupal_render($element['accession']) . "
         </div>
-        <div class=\"secondary-dbxref-widget-item\">" .
-          drupal_render($element['version']) . "
-        </div>
-        <div class=\"secondary-dbxref-widget-item\">" .
-          drupal_render($element['description']) . "
-        </div>
-        <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
       </div>
     ";
 
@@ -238,29 +165,6 @@ class sbo__database_cross_reference_widget extends ChadoFieldWidget {
 function sbo__database_cross_reference_widget_form_ajax_callback($form, $form_state) {
 
   $field_name = $form_state['triggering_element']['#parents'][0];
-  $delta = $form_state['triggering_element']['#parents'][2];
-  $field = field_info_field($field_name);
-  $field_type = $field['type'];
-  $instance = $form[$field_name]['und'][$delta]['#instance'];
-  $field_table = $instance['settings']['chado_table'];
-  $field_column = $instance['settings']['chado_column'];
-  $field_prefix = 'chado-' . $field_table . '__dbxref_id';
-
-  // Check to see if this dbxref already exists. If not then
-  // give a notice to the user that the dbxref will be added.
-  $db_id = $form_state['values'][$field_name]['und'][$delta]['db_id'];
-  $accession = $form_state['values'][$field_name]['und'][$delta]['accession'];
-  if ($db_id and $accession) {
-    $values = array(
-      'db_id' => $db_id,
-      'accession' => $accession,
-    );
-    $options = array('is_duplicate' => TRUE);
-    $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
-    if (!$has_duplicate) {
-      drupal_set_message('The selected cross reference is new and will be added.');
-    }
-  }
 
   return $form[$field_name]['und'][$delta];
 }

+ 2 - 2
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -628,8 +628,8 @@ function tripal_chado_field_storage_query($query) {
     } // end if ($sort['type'] == 'field') {
   } // end foreach ($query->order as $index => $sort) {
 
-//     dpm($cquery->__toString());
-//     dpm($cquery->getArguments());
+     dpm($cquery->__toString());
+     dpm($cquery->getArguments());
 
   $records = $cquery->execute();