Преглед на файлове

Cleaned up the way subfields of fields are handled and made consistent use of 'value' field

Stephen Ficklin преди 9 години
родител
ревизия
9917d5964e

+ 12 - 8
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -140,19 +140,24 @@ function theme_tripal_entities_date_combo($variables) {
  *
  */
 function tripal_entities_chado_field_alter(&$field) {
+
+  if (!array_key_exists('field_settings', $field)) {
+    return;
+  }
   // If the field doesn't list the Chado table or column then just return.
-  if (!array_key_exists('chado_table', $field) or !array_key_exists('chado_column', $field)) {
+  if (!array_key_exists('chado_table', $field['field_settings']) or
+      !array_key_exists('chado_column', $field['field_settings'])) {
     return;
   }
   // Here we provide new field types and widgets for FK fields
   // and fields that need special attention.
-  if ($field['chado_column'] =='organism_id') {
+  if ($field['field_settings']['chado_column'] =='organism_id') {
     $field['field_type'] = 'organism_id';
     $field['widget_type'] = 'tripal_fields_organism_select_widget';
     $field['label'] = 'Organism';
     $field['description'] = 'Select an organism.';
   }
-  else if ($field['chado_column'] =='dbxref_id') {
+  else if ($field['field_settings']['chado_column'] =='dbxref_id') {
     $field['field_type'] = 'dbxref_id';
     $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
     $field['label'] = 'Primary Cross Reference';;
@@ -162,21 +167,20 @@ function tripal_entities_chado_field_alter(&$field) {
         must be provided.  To remove a set reference, change the database
         field to "Select a Database".';
   }
-  else if ($field['chado_table'] == 'feature' and
-    $field['chado_column'] == 'md5checksum') {
+  else if ($field['field_settings']['chado_table'] == 'feature' and
+    $field['field_settings']['chado_column'] == 'md5checksum') {
     $field['field_type'] = 'md5checksum';
     $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
     $field['label'] = 'MD5 Checksum';
     $field['description'] = 'Generating MD5 checksum for the sequence.';
   }
-  else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'seqlen') {
+  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
     $field['field_type'] = 'seqlen';
     $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
     $field['label'] = 'Seqlen';
     $field['description'] = 'The length of the residues.';
-
   }
-  else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'residues') {
+  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
     $field['field_type'] = 'residues';
     $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
     $field['label'] = 'Residues';

+ 9 - 8
tripal_entities/includes/tripal_entities.entity_form.inc

@@ -152,7 +152,6 @@ function tripal_entities_entity_form($form, &$form_state, $term_name = '', $enti
     $form['#prefix'] = "<div id='$bundle_id-entity-form'>";
     $form['#suffix'] = "</div>";
   }
-
   return $form;
 }
 /**
@@ -211,7 +210,6 @@ function tripal_entities_entity_form_add_new($bundle_id, $cvterm, &$form, &$form
   // the Entity API to work. It must have a key of the entity name.
   $form_state[$cvterm->dbxref_id->db_id->name] = $entity;
 
-
 }
 /**
  * An Ajax callback for the tripal_entities_entity_form.
@@ -562,16 +560,18 @@ function tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $b
   $field_info = array(
     'field_type' => 'kvproperty_adder',
     'widget_type' => 'tripal_fields_kvproperty_adder_widget',
-    'field_settings' => array(),
+    'field_settings' => array(
+      'chado_table' => $kv_table . '_adder',
+      'chado_column' => '',
+    ),
     'widget_settings' => array('display_label' => 1),
     'description' => '',
     'label' => 'Additional Properties',
-    'chado_table' => $kv_table,
-    'chado_column' => '',
     'is_required' => 0,
   );
   tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
 }
+
 /**
  * Adds the fields for the base table to the entity.
  */
@@ -661,12 +661,13 @@ function tripal_entities_get_table_column_field_default($table_name, $schema, $c
   $field_info = array(
     'field_type' => '',
     'widget_type' => '',
-    'field_settings' => array(),
+    'field_settings' => array(
+      'chado_table' => $table_name,
+      'chado_column' => $column_name,
+    ),
     'widget_settings' => array('display_label' => 1),
     'description' => '',
     'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
-    'chado_table' => $table_name,
-    'chado_column' => $column_name,
     'is_required' => 0,
   );
 

+ 0 - 8
tripal_fields/api/tripal_fields.api.inc

@@ -22,14 +22,6 @@
  *     'cardinality' : indicates the number of values this field can support.
  *       the default is 1 (meaning only one value). Use a value of
  *       FIELD_CARDINALITY_UNLIMITED for unlimited number of values.
- *   You may add additional key/value pairs as needed by your custom module
- *   if your module adds a new field type.  The following fields are examples
- *   that are added automatically by the tripal_fields module.  These
- *   fields are expected when modules implement the hook_chado_field_alter()
- *   function:
- *     'chado_table' : the name of the table in Chado that will house the
- *       value(s) of this field.
- *     'chado_column' : the column in the Chado table where the field will go.
  * @param $entity_type_name
  *   The entity type name.
  * @param $bundle_name

+ 24 - 24
tripal_fields/includes/fields/kvproperty.inc

@@ -35,11 +35,10 @@ function tripal_fields_kvproperty_formatter(&$element, $entity_type, $entity, $f
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_kvproperty_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_kvproperty_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
   $chado_entity = $form['#entity']->chado_entity;
   $field_name = $field['field_name'];
-  $widget = $element;
 
   $matches = array();
   preg_match('/(.*?)__(\d+)/', $field_name, $matches);
@@ -73,28 +72,29 @@ function tripal_fields_kvproperty_widget($form, $form_state, $field, $instance,
   $widget['#element_validate'] = array('tripal_fields_kvproperty_widget_validate');
   $widget['#cardinality'] = 1;
 
-  $widget[$delta] = $element;
-  $widget[$delta] += array(
-    $table_name . '__' . $pkey => array(
-      '#type' => 'hidden',
-      '#value' => $pkey_value,
-    ),
-    $table_name . '__' . $fk => array(
-      '#type' => 'hidden',
-      '#value' => $fk_value,
-    ),
-    $table_name . '__value' => array(
-      '#type' => 'textfield',
-      '#default_value' => !empty($items) ? $items[0] : '',
-    ),
-    $table_name . '__type_id' => array(
-      '#type' => 'hidden',
-      '#value' => $cvterm_id,
-    ),
-    $table_name . '__rank' => array(
-      '#type' => 'hidden',
-      '#value' => $delta,
-    ),
+  $widget['value'][$delta] = array(
+    '#type' => 'hidden',
+    '#value' => $pkey_value,
+  );
+  $widget[$table_name . '__' . $pkey] = array(
+    '#type' => 'hidden',
+    '#value' => $pkey_value,
+  );
+  $widget[$table_name . '__' . $fk] = array(
+    '#type' => 'hidden',
+    '#value' => $fk_value,
+  );
+  $widget[$table_name . '__value'] = array(
+    '#type' => 'textfield',
+    '#default_value' => !empty($items) ? $items[0] : '',
+  );
+  $widget[$table_name . '__type_id'] = array(
+    '#type' => 'hidden',
+    '#value' => $cvterm_id,
+  );
+  $widget[$table_name . '__rank'] = array(
+    '#type' => 'hidden',
+    '#value' => $delta,
   );
 
   return $widget;

+ 42 - 43
tripal_fields/includes/fields/kvproperty_adder.inc

@@ -30,51 +30,50 @@ function tripal_fields_kvproperty_adder_formatter(&$element, $entity_type, $enti
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_kvproperty_adder_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_kvproperty_adder_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
-  $field_name = $field['field_name'];
-  $widget = $element;
-  $widget += array(
+  // This field has no value field.  Just a fieldset for adding new properties.
+
+  $widget['kvproperty_adder_fieldset'] = array(
     '#element_validate' => array('tripal_fields_kvproperty_adder_widget_validate'),
     '#type' => 'fieldset',
     '#title' => $element['#title'],
-    '#group' => 'ev_tabs',
-    array(
-      'kvproperty_instructions' => array(
-        '#type' => 'item',
-        '#markup' => t('You may add additional properties to this form by
-            providing a property name (from a vocabulary) in the field below
-            and clicking the "Add Property" button.  This will add a
-            new field to the form above for the property you entered.'),
-      ),
-      'kvproperty_adder' => array(
-        '#title' => t('Property Type'),
-        '#type' => 'textfield',
-        '#description' => t("Please enter the type of property that you want to add.  As you type, suggestions will be provided."),
-        '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/",
-      ),
-      'kvproperty_adder_link' => array(
-        '#type' => 'item',
-        '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/chado/tripal_cv/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default form submit.  In the validate function we set the form_state
-      // to rebuild the form so the submit function never actually gets called,
-      // but we need it or Drupal will run the default validate anyway.
-      // we also set #limit_validation_errors to empty so fields that
-      // are required that don't have values won't generate warnings.
-      'kvproperty_adder_button' => array(
-        '#value' => t('Add Property'),
-        '#type' => 'submit',
-        '#name' => 'kvproperty_adder_button',
-        '#validate' => 'tripal_fields_kvproperty_adder_widget_validate',
-        '#submit' => 'tripal_fields_kvproperty_adder_widget_submit',
-        '#limit_validation_errors' => array(array($field_name)),
-      ),
-    ),
+//    '#group' => 'ev_tabs',
+  );
+  $widget['kvproperty_adder_fieldset']['kvproperty_instructions'] = array(
+    '#type' => 'item',
+    '#markup' => t('You may add additional properties to this form by
+        providing a property name (from a vocabulary) in the field below
+        and clicking the "Add Property" button.  This will add a
+        new field to the form above for the property you entered.
+        In the future, this field will be present for all records
+        of this type.'),
+  );
+  $widget['kvproperty_adder_fieldset']['kvproperty_adder'] = array(
+    '#title' => t('Property Type'),
+    '#type' => 'textfield',
+    '#description' => t("Please enter the type of property that you want to add.  As you type, suggestions will be provided."),
+    '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/",
+  );
+  $widget['kvproperty_adder_fieldset']['kvproperty_adder_link'] = array(
+    '#type' => 'item',
+    '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/chado/tripal_cv/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
+  );
+  // When this button is clicked, the form will be validated and submitted.
+  // Therefore, we set custom submit and validate functions to override the
+  // default form submit.  In the validate function we set the form_state
+  // to rebuild the form so the submit function never actually gets called,
+  // but we need it or Drupal will run the default validate anyway.
+  // we also set #limit_validation_errors to empty so fields that
+  // are required that don't have values won't generate warnings.
+  $widget['kvproperty_adder_fieldset']['kvproperty_adder_button'] = array(
+    '#value' => t('Add Property'),
+    '#type' => 'submit',
+    '#name' => 'kvproperty_adder_button',
+    '#validate' => 'tripal_fields_kvproperty_adder_widget_validate',
+    '#submit' => 'tripal_fields_kvproperty_adder_widget_submit',
+    '#limit_validation_errors' => array(array($field['field_name'])),
   );
-  return $widget;
 }
 
 /**
@@ -93,7 +92,7 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
      $bundle = $element['#bundle'];
 
      // Get the term for the property
-     $kvproperty = tripal_fields_get_field_form_values($field_name, $form_state, 'kvproperty_adder');
+     $kvproperty = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'kvproperty_adder');
      $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
      if (count($term) == 1) {
        $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
@@ -111,10 +110,10 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
        tripal_add_bundle_field($prop_field_name, $field_info, $entity_type, $bundle);
      }
      else if (count($term) > 1) {
-       form_set_error(implode('][', $element ['#parents']) . '][0][kvproperty_adder', t("This term is present in multiple vocabularies. Please select the appropriate one."));
+       form_set_error(implode('][', $element ['#parents']) . '][kvproperty_adder', t("This term is present in multiple vocabularies. Please select the appropriate one."));
      }
      else {
-       form_set_error(implode('][', $element ['#parents']) . '][0][kvproperty_adder', t("Please provide a property type to add."));
+       form_set_error(implode('][', $element ['#parents']) . '][kvproperty_adder', t("Please provide a property type to add."));
      }
    }
 }

+ 6 - 8
tripal_fields/includes/fields/md5checksum.inc

@@ -34,11 +34,9 @@ function tripal_fields_md5checksum_checkbox_formatter(&$element, $entity_type, $
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_md5checksum_checkbox_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_md5checksum_checkbox_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
-  $field_name = $field['field_name'];
-  $widget = $element;
-  $widget +=  array(
+  $widget['value'] = array(
     '#type' => 'checkbox',
     '#title' => $element['#title'],
     '#description' => $element['#description'],
@@ -48,19 +46,19 @@ function tripal_fields_md5checksum_checkbox_widget($form, $form_state, $field, $
     '#delta' => $delta,
     '#element_validate' => array('tripal_fields_md5checksum_checkbox_widget_validate'),
   );
-  return $widget;
 }
 /**
  * Callback function for validating the tripal_fields_md5checksum_checkbox_widget.
  */
 function tripal_fields_md5checksum_checkbox_widget_validate($element, &$form_state) {
-  $field_name = $element['#field_name'];
+  $field_name = $element['#parents'][0];
 
   // Calculate the md5 checksum for the sequence only if md5 box is checked and
   // the residues exist.
   $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
-  if (count($residues) > 0 && trim($residues[0]) != '') {
-    tripal_fields_set_field_form_values($field_name, $form_state, md5($residues[0]));
+  if ($residues) {
+    $residues = preg_replace('/\s/', '', $residues);
+    tripal_fields_set_field_form_values($field_name, $form_state, md5($residues));
   }
   else {
     // Otherwise, remove the md5 value

+ 10 - 9
tripal_fields/includes/fields/organism_id.inc

@@ -35,11 +35,10 @@ function tripal_fields_organism_select_formatter(&$element, $entity_type, $entit
  * @param $delta
  * @param $element
  */
-function tripal_fields_organism_select_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
-  $field_name = $field['field_name'];
-  $options = tripal_get_organism_select_options();
-  $widget = $element;
-  $widget += array(
+function tripal_fields_organism_select_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+
+  $options = tripal_get_organism_select_options(FALSE);
+  $widget['value'] = array(
     '#type' => 'select',
     '#title' => $element['#title'],
     '#description' => $element['#description'],
@@ -50,14 +49,16 @@ function tripal_fields_organism_select_widget($form, $form_state, $field, $insta
     '#delta' => $delta,
     '#element_validate' => array('tripal_fields_organism_select_widget_validate'),
   );
-  return $widget;
+  $widget['add_organism'] = array(
+    '#type' => 'item',
+    '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
+  );
 }
-
 /**
  * Callback function for validating the tripal_fields_organism_select_widget.
  */
 function tripal_fields_organism_select_widget_validate($element, &$form_state) {
-  $field_name = $element['#field_name'];
+  $field_name = $element['#parents'][0];
 
   // If the form ID is field_ui_field_edit_form, then the user is editing the
   // field's values in the manage fields form of Drupal.  We don't want
@@ -67,7 +68,7 @@ function tripal_fields_organism_select_widget_validate($element, &$form_state) {
   }
   $organism_id = tripal_fields_get_field_form_values($field_name, $form_state);
 
-  if (count($organism_id) == 0) {
+  if (!$organism_id) {
     form_error($element, t("Please specify an organism."));
   }
 }

+ 107 - 91
tripal_fields/includes/fields/primary_dbxref.inc

@@ -41,9 +41,8 @@ function tripal_fields_primary_dbxref_formatter(&$element, $entity_type, $entity
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_primary_dbxref_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
-  $widget = $element;
   $field_name = $field['field_name'];
 
   // Get the field defaults from the database if a record exists.
@@ -60,10 +59,8 @@ function tripal_fields_primary_dbxref_widget($form, $form_state, $field, $instan
     $version = $dbxref->version;
     $description = $dbxref->description;
   }
-  $temp = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
-  if (count($temp) > 0) {
-    $db_id = $temp[0];
-  }
+  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
+
   // If we are here because our parent was triggered in a form submit
   // then that means an ajax call was made and we don't want the fieldset to
   // be closed when it returns from the ajax call.
@@ -78,84 +75,84 @@ function tripal_fields_primary_dbxref_widget($form, $form_state, $field, $instan
 
   $schema = chado_get_schema('dbxref');
   $options = tripal_get_db_select_options();
-  $widget +=  array(
-    '#element_validate' => array('tripal_fields_primary_dbxref_widget_validate'),
-    '#type' => 'fieldset',
+
+  $widget['#element_validate'] = array('tripal_fields_primary_dbxref_widget_validate');
+  $widget['#theme'] = 'tripal_fields_primary_dbxref_widget';
+  $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id'>";
+  $widget['#suffix'] =  "</span>";
+
+  // A temporary element used for theming the fieldset.
+  $widget['#theme_settings'] = array(
     '#title' => $element['#title'],
     '#description' =>  $element['#description'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
-    '#delta' => $delta,
     '#theme' => 'tripal_fields_primary_dbxref_widget',
-    '#group' => 'entity_vetical_tabs',
     '#collapsible' => TRUE,
     '#collapsed' => $collapsed,
-    array(
-      $element['#field_name'] => array(
-        '#type' => 'hidden',
-        '#default_value' => $dbxref_id,
-      ),
-      'dbxref__db_id' => array(
-        '#type' => 'select',
-        '#title' => t('Database'),
-        '#options' => $options,
-        '#required' => $element['#required'],
-        '#default_value' => $db_id,
-        '#ajax' => array(
-          'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
-          'wrapper' => "$field_name-dbxref--db-id",
-          'effect' => 'fade',
-          'method' => 'replace'
-        )
-      ),
-      'dbxref__accession' => array(
-        '#type' => 'textfield',
-        '#title' => t('Accession'),
-        '#default_value' => $accession,
-        '#required' => $element['#required'],
-        '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
-        '#size' => 15,
-        '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
-        '#ajax' => array(
-          'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
-          'wrapper' => "$field_name-dbxref--db-id",
-          'effect' => 'fade',
-          'method' => 'replace'
-        )
-      ),
-      'dbxref__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,
-      ),
-      'dbxref__description' => array(
-        '#type' => 'textfield',
-        '#title' => t('Description'),
-        '#default_value' => $description,
-        '#size' => 20,
-      ),
-      'links' => array(
-        '#type' => 'item',
-        '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attrbutes', array('target' => '_blank')))
-      )
+  );
+
+  $widget['value'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $dbxref_id,
+  );
+
+  $widget['dbxref__db_id'] = array(
+    '#type' => 'select',
+    '#title' => t('Database'),
+    '#options' => $options,
+    '#required' => $element['#required'],
+    '#default_value' => $db_id,
+    '#ajax' => array(
+      'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
+      'wrapper' => "$field_name-dbxref--db-id",
+      'effect' => 'fade',
+      'method' => 'replace'
     ),
-    '#prefix' => "<span id='$field_name-dbxref--db-id'>",
-    '#suffix' => "</span>"
   );
-  return $widget;
+  $widget['dbxref__accession'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Accession'),
+    '#default_value' => $accession,
+    '#required' => $element['#required'],
+    '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
+    '#size' => 15,
+    '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
+    '#ajax' => array(
+      'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
+      'wrapper' => "$field_name-dbxref--db-id",
+      'effect' => 'fade',
+      'method' => 'replace'
+    )
+  );
+  $widget['dbxref__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,
+  );
+  $widget['dbxref__description'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Description'),
+    '#default_value' => $description,
+    '#size' => 20,
+  );
+  $widget['links'] = array(
+    '#type' => 'item',
+    '#markup' => l('Add a new database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
+  );
 }
 /**
  * An Ajax callback for the tripal_fields_admin_publish_form..
  */
 function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
   $field_name = $form_state['triggering_element']['#parents'][0];
-  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
-  $accession = tripal_fields_get_field_form_values($field_name, $form_state, 'dbxref__accession');
-  if (count($db_id) > 0 and count($accession) > 0) {
+  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
+  $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
+  if ($db_id and $accession) {
     $values = array(
-      'db_id' => $db_id[0],
-      'accession' => $accession[0],
+      'db_id' => $db_id,
+      'accession' => $accession,
     );
     $options = array('is_duplicate' => TRUE);
     $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
@@ -171,7 +168,7 @@ function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_sta
  * Callback function for validating the tripal_fields_organism_select_widget.
  */
 function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
-  $field_name = $element['#field_name'];
+  $field_name = $element['#parents'][0];
 
   // If the form ID is field_ui_field_edit_form, then the user is editing the
   // field's values in the manage fields form of Drupal.  We don't want
@@ -181,11 +178,11 @@ function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
   }
 
   // Get the field values.
-  $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, $field_name);
-  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__db_id");
-  $accession = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__accession");
-  $version = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__version");
-  $description = tripal_fields_get_field_form_values($field_name, $form_state, "dbxref__description");
+  $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, $field_name);
+  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
+  $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
+  $version = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
+  $description = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__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
@@ -194,25 +191,28 @@ function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
   // clear to the user what field is required and which isn't. Therefore,
   // we borrow the code from the 'form_error' function and append the field
   // so that the proper field is highlighted on error.
-  if (count($db_id) == 0 and count($accession) > 0) {
-    form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
+  if (!$db_id and $accession) {
+    form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
+  }
+  if ($db_id and !$accession) {
+    form_set_error(implode('][', $element ['#parents']) . '][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
   }
-  if (count($db_id) > 0 and count($accession) == 0) {
-    form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
+  if (!$db_id and !$accession and ($version or $description)) {
+    form_set_error(implode('][', $element ['#parents']) . '][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
   }
 
   // If user did not select a database, we want to remove dbxref_id from the
   // field.
-  if (count($db_id) == 0) {
-    tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__', $field_name);
+  if (!$db_id) {
+    tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
   }
   // 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 (count($db_id) == 1 and count($accession) == 1) {
-    $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id[0], 'accession' => $accession[0]));
-    if ($dbxref->dbxref_id != $dbxref_id[0]) {
-      tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $field_name);
+  if ($db_id and $accession) {
+    $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
+    if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
+      tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
     }
   }
 }
@@ -223,22 +223,38 @@ function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
  */
 function theme_tripal_fields_primary_dbxref_widget($variables) {
   $element = $variables['element'];
+
   $layout = "
     <div class=\"primary-dbxref-widget\">
       <div class=\"primary-dbxref-widget-item\">" .
-      drupal_render($element[0]['dbxref__db_id']) . "
+        drupal_render($element['dbxref__db_id']) . "
       </div>
       <div class=\"primary-dbxref-widget-item\">" .
-      drupal_render($element[0]['dbxref__accession']) . "
+        drupal_render($element['dbxref__accession']) . "
       </div>
       <div class=\"primary-dbxref-widget-item\">" .
-      drupal_render($element[0]['dbxref__version']) . "
+        drupal_render($element['dbxref__version']) . "
       </div>
       <div class=\"primary-dbxref-widget-item\">" .
-      drupal_render($element[0]['dbxref__description']) . "
+        drupal_render($element['dbxref__description']) . "
       </div>
-      <div class=\"primary-dbxref-widget-links\">" . drupal_render($element[0]['links']) . "</div>
+      <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
     </div>
   ";
-      return $layout;
+
+  $classes = array();
+  $classes[] = 'collapsible';
+  $theme_settings = $element['#theme_settings'];
+  if ($theme_settings['#collapsed'] == TRUE) {
+    $classes[] = 'collapsed';
+  }
+  $fieldset = array(
+    '#title' => $element['#title'],
+    '#value' => '',
+    '#description' => $element['#description'],
+    '#children' => $layout,
+    '#attributes' => array('class' => $classes),
+  );
+
+  return theme('fieldset', array('element' => $fieldset));
 }

+ 5 - 9
tripal_fields/includes/fields/residues.inc

@@ -37,11 +37,9 @@ function tripal_fields_residues_textarea_formatter(&$element, $entity_type, $ent
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_residues_textarea_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_residues_textarea_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
-  $field_name = $field['field_name'];
-  $widget = $element;
-  $widget += array(
+  $widget['value'] = array(
     '#type' => 'textarea',
     '#title' => $element['#title'],
     '#description' => $element['#description'],
@@ -50,19 +48,17 @@ function tripal_fields_residues_textarea_widget($form, $form_state, $field, $ins
     '#delta' => $delta,
     '#element_validate' => array('tripal_fields_residues_textarea_widget_validate'),
   );
-   return $widget;
-
 }
 /**
  * Callback function for validating the tripal_fields_residues_textarea_widget.
  */
 function tripal_fields_residues_textarea_widget_validate($element, &$form_state) {
-  $field_name = $element['#field_name'];
+  $field_name = $element['#parents'][0];
 
   // Remove any white spaces.
   $residues = tripal_fields_get_field_form_values($field_name, $form_state);
-  if (count($residues) > 0) {
-    $residues = preg_replace('/\s/', '', $residues[0]);
+  if ($residues) {
+    $residues = preg_replace('/\s/', '', $residues);
     tripal_fields_set_field_form_values($field_name, $form_state, $residues);
   }
 }

+ 5 - 8
tripal_fields/includes/fields/seqlen.inc

@@ -36,11 +36,9 @@ function tripal_fields_seqlen_hidden_formatter(&$element, $entity_type, $entity,
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_seqlen_hidden_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_seqlen_hidden_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
-  $field_name = $field['field_name'];
-  $widget = $element;
-  $widget +=  array(
+  $widget['value'] =  array(
     '#type' => 'hidden',
     '#title' => $element['#title'],
     '#description' => $element['#description'],
@@ -48,20 +46,19 @@ function tripal_fields_seqlen_hidden_widget($form, $form_state, $field, $instanc
     '#delta' => $delta,
     '#element_validate' => array('tripal_fields_seqlen_hidden_widget_validate'),
   );
-  return $widget;
 }
 /**
  * Callback function for validating the tripal_fields_seqlen_hidden_widget.
  */
 function tripal_fields_seqlen_hidden_widget_validate($element, &$form_state) {
-  $field_name = $element['#field_name'];
+  $field_name = $element['#parents'][0];
 
   // Get the residues so we can calculate teh length.
   $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
 
   // Remove any white spaces.
-  if (count($residues) > 0) {
-    $residues = preg_replace('/\s/', '', $residues[0]);
+  if ($residues) {
+    $residues = preg_replace('/\s/', '', $residues);
     tripal_fields_set_field_form_values($field_name, $form_state, strlen($residues));
   }
   else {

+ 58 - 91
tripal_fields/includes/tripal_fields.field_storage.inc

@@ -27,6 +27,7 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
 
   // Convert the fields into a key/value list of fields and their values.
   $field_vals = tripal_fields_field_storage_unnest_fields($fields, $entity_type, $entity);
+
   $data_table = NULL;
   $type_field = NULL;
   $record_id = NULL;
@@ -86,25 +87,24 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
   // Recursively write fields for any referring tables that have
   // values in the $field_vals array.  There should be a key for each
   // FK table in the $field_vals array.
-  $schema = chado_get_schema($data_table);
-  $rtables = $schema['referring_tables'];
-  dpm($schema);
-  foreach ($rtables as $rtable) {
-    dpm($rtable);
-    if (array_key_exists($rtable, $field_vals)) {
-      tripal_fields_field_storage_write_recursive($entity_type, $entity,
-          $op, $field_vals, $rtable, $type_field, $record_id, 1);
-    }
-  }
+//   $schema = chado_get_schema($data_table);
+//   $rtables = $schema['referring_tables'];
+//   foreach ($rtables as $rtable) {
+//     if (array_key_exists($rtable, $field_vals)) {
+//       tripal_fields_field_storage_write_recursive($entity_type, $entity,
+//           $op, $field_vals, $rtable, NULL, NULL, 1);
+//     }
+//   }
 }
 
 /**
  * Implements hook_field_storage_write_recursive().
  */
 function tripal_fields_field_storage_write_recursive($entity_type, $entity,
-   $op, $field_vals, $tablename, $type_field = NULL, $record_id = NULL, $depth = 0) {
+   $op, $field_vals, $tablename, $type_field = NULL,
+   $record_id = NULL, $depth = 0) {
 
-  // Intialize the values array and $record_id;
+  // Intialize the values array.
   $values = array();
 
   // Get the schema for this table so that we can identify the primary key
@@ -132,22 +132,12 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
 
       // Get the value of the FK field as provided by the user.
       $fk_val = NULL;
+      $fk_vals = array();
       $fk_field_name = $tablename . '__' . $local_id;
-
-      // If this is the base table then we'll find any values for the FK
-      // fields as any other field.  IF we are not at the base table,
-      // then the FK values are in a sub element of the array where the
-      // key of the field is the table name. Because we are looping
-      // on FKs that are members of the current table we should always only
-      // have one value.
-      if ($depth == 0) {
-        $fk_val = array_key_exists($fk_field_name, $field_vals) ? $field_vals[$fk_field_name] : NULL;
-      }
-      else {
-        if (array_key_exists($fk_table, $field_vals) and
-            array_key_exists($fk_field_name, $field_vals[$fk_table])) {
-          $fk_val = $field_vals[$fk_table][$fk_field_name];
-        }
+      if (array_key_exists($fk_field_name, $field_vals)) {
+        $fk_val = $field_vals[$fk_field_name][0]['value'];
+        $fk_vals = $field_vals[$fk_field_name][0];
+        unset($fk_vals['value']);
       }
 
       // Don't recurse if the value of the FK field is set to NULL.  The
@@ -156,28 +146,32 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
         $values[$local_id] = $fk_val;
         continue;
       }
+
+      // Don't recuse if there are no values to select/insert a foreign key.
+      if (count(array_keys($fk_vals)) == 0) {
+        $values[$local_id] = $fk_val;
+        continue;
+      }
+
       // Recurse on the FK field.  Pass in the ID for the FK field if one
       // exists in the $field_vals;
       $fk_val = tripal_fields_field_storage_write_recursive($entity_type,
-        $entity, $op, $field_vals, $fk_table, NULL, $fk_val, $depth + 1);
+        $entity, $op, $fk_vals, $fk_table, NULL, $fk_val, $depth + 1);
       if (isset($fk_val) and $fk_val != '' and $fk_val != 0) {
         $values[$local_id] = $fk_val;
       }
     }
   }
 
-  // STEP 2: Loop through the incoming fields.
+  // STEP 2: Loop through the non FK fields.
   // Loop through the fields passed to the function and find any that
   // are for this table.  Then add their values to the $values array.
-  foreach ($field_vals as $field_name => $field_val) {
-    // If the field value is empty then continue.
-    if (!isset($field_val) or $field_val === '' or $field_val === NULL) {
-      continue;
-    }
+  foreach ($field_vals as $field_name => $items) {
+
     if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
       $chado_field = $matches[1];
 
-      // Skip the Pkey field. We won't ever insert a primary key and if
+      // Skip the PKey field. We won't ever insert a primary key and if
       // one is provided in the fields then we use it for matching on an
       // update.  We don't add it to the $values array in either case.
       if ($chado_field == $pkey_field) {
@@ -191,7 +185,7 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
       }
 
       // Add the value of the field to the $values arr for later insert/update.
-      $values[$chado_field] = $field_vals[$field_name];
+      $values[$chado_field] = $items[0]['value'];
     }
   }
 
@@ -222,19 +216,19 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
     // Add a record to the chado_entity table so that the data for the
     // fields can be pulled from Chado when loaded the next time. Only do
     // this for the base table record.
-    if ($depth == 0) {
-      $record = array(
-        'entity_id' => $entity->id,
-        'record_id' => $record_id,
-        'data_table' => $tablename,
-        'type_table' => $tablename, // TODO: this must be fixed.
-        'field' => $type_field,
-      );
-      $success = drupal_write_record('chado_entity', $record);
-      if (!$success) {
-        drupal_set_message('Unable to insert new Chado entity.', 'error');
+      if ($depth == 0) {
+        $record = array(
+          'entity_id' => $entity->id,
+          'record_id' => $record_id,
+          'data_table' => $tablename,
+          'type_table' => $tablename, // TODO: this must be fixed.
+          'field' => $type_field,
+        );
+        $success = drupal_write_record('chado_entity', $record);
+        if (!$success) {
+          drupal_set_message('Unable to insert new Chado entity.', 'error');
+        }
       }
-    }
   }
   // We have an incoming record_id so this is an update.
   else {
@@ -243,6 +237,7 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
       drupal_set_message("Could not update Chado record in table: $tablename.", 'error');
     }
   }
+
   return $record_id;
 }
 
@@ -322,54 +317,26 @@ function tripal_fields_field_storage_load($entity_type, $entities, $age, $fields
 function tripal_fields_field_storage_unnest_fields($fields, $entity_type, $entity) {
   $new_fields = array();
 
+  // Iterate through all of the fields.
   foreach ($fields as $field_id => $ids) {
+
+    // Get the field name and all of it's items.
     $field = field_info_field_by_id($field_id);
     $field_name = $field['field_name'];
-
-    // Currently, we only support one language, but for for the sake of
-    // thoroughness we'll iterate through all possible languages.
-    $all_languages = field_available_languages($entity_type, $field);
-    $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
-    foreach ($field_languages as $langcode) {
-
-      // The number of items is related to the cardinatily of the field.
-      $items = (array) $entity->{$field_name}[$langcode];
-      foreach ($items as $delta => $item) {
-        // There must be a 'value' field.
-        if (!array_key_exists('value', $item)) {
-          continue;
-        }
-
-        // Case 1: base table fields.
-        // If the value is not an array then we can just use the value as is.
-        // This occurs for fields of the base table.
-        if (!is_array($item['value'])) {
-          $new_fields[$field_name] = $item['value'];
+    $items = field_get_items($entity_type, $entity, $field_name);
+
+    // Iterate through the field's items. Fields with cardinality ($delta) > 1
+    // are multi-valued.
+    foreach ($items as $delta => $item) {
+      foreach ($item as $item_name => $value) {
+        if ($item_name == 'value') {
+          $new_fields[$field_name][$delta]['value'] = $value;
           continue;
         }
-
-        // Case 2: foreign key fields.
-        // If the value is an array then there are sub fields. This occurs
-        // for fields that represent FKs with the base table.
-        $i = 0;
-        foreach ($item['value'] as $children) {
-          foreach ($children as $child_field_name => $child_value) {
-            $matches = array();
-            if (preg_match('/^(.*?)__.*?$/', $child_field_name, $matches)) {
-              $table_name = $matches[1];
-              // Case 2a: the FK field is in the basee table.
-              if ($table_name == $entity->chado_entity->data_table) {
-                $new_fields[$child_field_name] = $child_value;
-              }
-              // Case 2b: the fields of the FK relationship are in the non
-              // base table. We store these in an array with the key being
-              // the table name.
-              else {
-                $new_fields[$table_name][$i][$child_field_name] = $child_value;
-              }
-            }
-          }
-          $i++;
+        $matches = array();
+        if (preg_match('/^(.*?)__.*?$/', $item_name, $matches)) {
+          $table_name = $matches[1];
+          $new_fields[$field_name][$delta][$item_name][0]['value'] = $value;
         }
       }
     }

+ 61 - 41
tripal_fields/tripal_fields.module

@@ -235,54 +235,48 @@ function tripal_fields_field_formatter_view($entity_type, $entity, $field,
 function tripal_fields_field_widget_form(&$form, &$form_state, $field,
     $instance, $langcode, $items, $delta, $element) {
 
+  $widget = $element;
   switch ($instance['widget']['type']) {
     case 'tripal_fields_organism_select_widget':
       // Make sure the include files get parsed now and for the form submits.
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/organism_id');
       module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
       // Update the widget with the new field.
-      $element['value'] = tripal_fields_organism_select_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_primary_dbxref_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/primary_dbxref');
       module_load_include('inc', 'tripal_fields', 'includes/fields/primary_dbxref');
-      $element['value'] = tripal_fields_primary_dbxref_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_primary_dbxref_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_md5checksum_checkbox_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/md5checksum');
       module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
-      $element['value'] = tripal_fields_md5checksum_checkbox_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_residues_textarea_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/residues');
       module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
-      $element['value'] = tripal_fields_residues_textarea_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_seqlen_hidden_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/seqlen');
       module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
-      $element['value'] = tripal_fields_seqlen_hidden_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_kvproperty_adder_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
       module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
-      $element['value'] = tripal_fields_kvproperty_adder_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
     case 'tripal_fields_kvproperty_widget':
       form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty');
       module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
-      $element['value'] = tripal_fields_kvproperty_widget($form,
-          $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      tripal_fields_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
       break;
   }
 
-  return $element;
+  return $widget;
 }
 
 
@@ -290,51 +284,77 @@ function tripal_fields_field_widget_form(&$form, &$form_state, $field,
  * Implements hook_field_is_empty().
  */
 function tripal_fields_field_is_empty($item, $field) {
-  if (empty($item['value']) && (string) $item['value'] !== '0') {
+  // If there is no value field then the field is empty.
+  if (!array_key_exists('value', $item)) {
     return TRUE;
   }
-  return FALSE;
+
+  // Iterate through all of the fields and if at least one has a value
+  // the field is not empty.
+  foreach ($item as $form_field_name => $value) {
+    if (isset($value) and $value != NULL and $value != '') {
+      return FALSE;
+    }
+  }
+
+  // Otherwise, the field is empty.
+  return TRUE;
 }
 
 /**
  * Returns the values of the field from the $form_state.
  */
-function tripal_fields_get_field_form_values($field_name, $form_state, $child = NULL) {
-  $values = array();
+function tripal_fields_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
+
+  $value = NULL;
+  // The form_state must have the 'values' key. If not then just return.
   if (!array_key_exists('values', $form_state)) {
-    return $values;
+    return $value;
   }
-  if (array_key_exists($field_name, $form_state['values'])) {
-    foreach ($form_state['values'][$field_name] as $langcode => $items) {
-      foreach ($items as $delta => $value) {
-        if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
-          $values[] = $value['value'][0][$child];
-        }
-        else if (!$child and is_array($value) and $value['value']) {
-          $values[] = $value['value'];
-        }
+
+  // If the field name is not in the form_state['values'] then return.
+  if (!array_key_exists($field_name, $form_state['values'])) {
+    return $value;
+  }
+
+  // Iterate through the values looking for the field_name provided.
+  foreach ($form_state['values'][$field_name] as $langcode => $items) {
+    $item = $items[$delta];
+    if ($child){
+      if(array_key_exists($child, $item) and $item[$child] != '') {
+        $value = $item[$child];
       }
     }
+    else {
+      $value = $item['value'];
+    }
   }
-  return $values;
+  return $value;
 }
 
 /**
- * Returns the values of the field from the $form_state.
+ * Sets the values of the field from the $form_state.
  */
-function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
-  $values = array();
+function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
+  // The form_state must have the 'values' key. If not then just return.
+  if (!array_key_exists('values', $form_state)) {
+    return FALSE;
+  }
+
+  // If the field name is not in the form_state['values'] then reutrn.
+  if (!array_key_exists($field_name, $form_state['values'])) {
+    return FALSE;
+  }
+
   foreach ($form_state['values'][$field_name] as $langcode => $items) {
-    foreach ($items as $delta => $value) {
-      if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
-        $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
-      }
-      else if (!$child) {
-        $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
-      }
+    if ($child) {
+      $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
+    }
+    else {
+      $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
     }
   }
-  return $values;
+  return TRUE;
 }
 
 

+ 0 - 7
tripal_organism/theme/templates/tripal_organism_base.tpl.php

@@ -1,12 +1,5 @@
 <?php
 
-//$cv = entity_load('trp_vocabulary', array(20, 21, 22));
-//$cvterm = entity_load('trp_vocabulary_term', array(489, 44099, 45222, 527));
-//$gene = entity_load('trp_SO_0000704', array(3));
-//dpm($cv);
-//dpm($cvterm);
-//dpm($gene);
-
 $organism  = $variables['node']->organism;
 $organism = chado_expand_var($organism,'field','organism.comment'); ?>
 

+ 12 - 13
tripal_pub/includes/importers/tripal_pub.AGL.inc

@@ -1,16 +1,16 @@
 <?php
 /**
  * @file
- * This file provides support for importing and parsing of results from the 
+ * This file provides support for importing and parsing of results from the
  * USDA National Agricultural Library (AGL) database.  The functions here are used by
  * both the publication importer setup form and the publication importer.
- * 
+ *
  * The USDA AGL database uses a YAZ protocol for querying and retrieving records.
- * 
+ *
  */
 
 /**
- * A hook for altering the publication importer form.  It Changes the 
+ * A hook for altering the publication importer form.  It Changes the
  * 'Days' element to 'Year' and removes the 'Journal Name' filter.
  *
  * @param $form
@@ -47,7 +47,7 @@ function tripal_pub_remote_alter_form_AGL($form, $form_state, $num_criteria = 1)
  *   The Drupal form array
  * @param $form_state
  *   The form state array
- *  
+ *
  * @return
  *  The form (drupal form api)
  *
@@ -414,9 +414,9 @@ function tripal_pub_remote_search_AGL($search_array, $num_to_retrieve, $page) {
  * @param $num_to_retrieve
  *   The number of publications to retrieve
  * @param $total_records
- *   The total number of records in the dataset.  This value should have 
+ *   The total number of records in the dataset.  This value should have
  *   been retrieved by tripal_pub_AGL_count() function.
- *   
+ *
  * @return
  *  An array containing the total_records in the dataaset, the search string
  *  and an array of the publications that were retreived.
@@ -468,7 +468,7 @@ function tripal_pub_AGL_range($yazc, $search_str, $start, $num_to_retrieve, $tot
  *   The YAZC connection object.
  * @param $search_str
  *   The search string to use for searching.
- *   
+ *
  * @return
  *   a count of the total number of publications that match the search string
  *
@@ -859,7 +859,6 @@ function tripal_pub_AGL_parse_pubxml($pub_xml) {
       }
     }
   }
-  //dpm($unhandled);
 
   // build the Dbxref
   if ($pub['Publication Database'] != 'AGL') {
@@ -918,12 +917,12 @@ function tripal_pub_AGL_parse_pubxml($pub_xml) {
   $pub['Citation'] = tripal_pub_create_citation($pub);
 
   $pub['raw'] = $pub_xml;
-  
+
   return $pub;
 }
 
 /**
- * Used for parsing of the XML results to get a set of subfields 
+ * Used for parsing of the XML results to get a set of subfields
  *
  * @param $xml
  *   The XMl object to read
@@ -952,14 +951,14 @@ function tripal_pub_remote_search_AGL_get_subfield($xml) {
 }
 
 /**
- * Used for parsing of the XML results to get details about an author 
+ * Used for parsing of the XML results to get details about an author
  *
  * @param $xml
  *   The XML object to read
  * @param $ind1
  *   Indicates how an author record is stored; 0 means given name is first
  *   1 means surname is first, 3 means a family name is given
- *   
+ *
  * @return
  *
  *

+ 11 - 14
tripal_pub/includes/importers/tripal_pub.PMID.inc

@@ -1,15 +1,15 @@
 <?php
 /**
  * @file
- * This file provides support for importing and parsing of results from the 
+ * This file provides support for importing and parsing of results from the
  * NCBI PubMed database.  The functions here are used by
  * both the publication importer setup form and the publication importer.
- * 
+ *
  */
 
 
 /**
- * A hook for altering the publication importer form.  It Changes the 
+ * A hook for altering the publication importer form.  It Changes the
  * 'Abstract' filter to be 'Abstract/Title'.
  *
  * @param $form
@@ -41,7 +41,7 @@ function tripal_pub_remote_alter_form_PMID($form, $form_state, $num_criteria = 1
  *   The Drupal form array
  * @param $form_state
  *   The form state array
- *  
+ *
  * @return
  *  The form (drupal form api)
  *
@@ -193,8 +193,8 @@ function tripal_pub_remote_search_PMID($search_array, $num_to_retrieve, $page) {
  *   The PubMed Search string
  * @param $retmax
  *   The maximum number of records to return
- *   
- * @return 
+ *
+ * @return
  *   An array containing the Count, WebEnv and QueryKey as return
  *   by PubMed's esearch utility
  *
@@ -211,7 +211,6 @@ function tripal_pub_PMID_search_init($search_str, $retmax){
     "&usehistory=y".
     "&term=" . urlencode($search_str);
 
-  //dpm($query_url);
   $rfh = fopen($query_url, "r");
   if (!$rfh) {
     drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
@@ -226,7 +225,6 @@ function tripal_pub_PMID_search_init($search_str, $retmax){
     $query_xml .= fread($rfh, 255);
   }
   fclose($rfh);
-  //dpm("<pre>$query_xml</pre>");
   $xml = new XMLReader();
   $xml->xml($query_xml);
 
@@ -270,7 +268,7 @@ function tripal_pub_PMID_search_init($search_str, $retmax){
  * @param $web_env
  *   The esearch WebEnv
  * @param $rettype
- *   The efetch return type 
+ *   The efetch return type
  * @param $retmod
  *   The efetch return mode
  * @param $start
@@ -279,7 +277,7 @@ function tripal_pub_PMID_search_init($search_str, $retmax){
  *   The number of publications to retrieve
  * @param $args
  *   Any additional arguments to add the efetch query URL
- *   
+ *
  * @return
  *  An array containing the total_records in the dataaset, the search string
  *  and an array of the publications that were retreived.
@@ -312,7 +310,6 @@ function tripal_pub_PMID_fetch($query_key, $web_env, $rettype = 'null',
       $fetch_url .= "&$key=$value";
     }
   }
-  //dpm($fetch_url);
   $rfh = fopen($fetch_url, "r");
   if (!$rfh) {
     drupal_set_message('ERROR: Could not perform PubMed query.', 'error');
@@ -445,7 +442,7 @@ function tripal_pub_PMID_parse_pubxml($pub_xml) {
 
 /**
  * Parses the section from the XML returned from PubMed that contains
- * information about the Journal 
+ * information about the Journal
  *
  * @param $xml
  *   The XML to parse
@@ -612,7 +609,7 @@ function tripal_pub_PMID_parse_publication_type($xml, &$pub) {
         case 'PublicationType':
           $xml->read();
           $value = $xml->value;
-          
+
           $identifiers = array(
             'name' => $value,
             'cv_id' => array(
@@ -631,7 +628,7 @@ function tripal_pub_PMID_parse_publication_type($xml, &$pub) {
             );
             $pub_cvterm = tripal_get_cvterm($identifiers, $options);
             if (!$pub_cvterm) {
-              tripal_report_error('tripal_pubmed', TRIPAL_ERROR, 
+              tripal_report_error('tripal_pubmed', TRIPAL_ERROR,
                 'Cannot find a valid vocabulary term for the publication type: "%term".',
                 array('%term' => $value));
             }