Browse Source

Cleaning up loading of fields

Stephen Ficklin 9 năm trước cách đây
mục cha
commit
18cf2abf2c

+ 1 - 4
tripal_entities/includes/tripal_entities.entity_form.inc

@@ -560,10 +560,7 @@ 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(
-      'chado_table' => $kv_table . '_adder',
-      'chado_column' => '',
-    ),
+    'field_settings' => array(),
     'widget_settings' => array('display_label' => 1),
     'description' => '',
     'label' => 'Additional Properties',

+ 54 - 27
tripal_fields/includes/fields/primary_dbxref.inc → tripal_fields/includes/fields/dbxref_id.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_primary_dbxref_formatter(&$element, $entity_type, $entity, $field,
+function tripal_fields_dbxref_id_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
   foreach ($items as $delta => $item) {
@@ -41,25 +41,16 @@ function tripal_fields_primary_dbxref_formatter(&$element, $entity_type, $entity
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
   $field_name = $field['field_name'];
 
-  // Get the field defaults from the database if a record exists.
-  $dbxref_id = '';
-  $db_id = '';
-  $accession = '';
-  $version = '';
-  $description = '';
-  if (count($items) > 0 and $items[0]['value']) {
-    $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
-    $dbxref_id = $dbxref->dbxref_id;
-    $db_id = $dbxref->db_id->db_id;
-    $accession  = $dbxref->accession;
-    $version = $dbxref->version;
-    $description = $dbxref->description;
-  }
-  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
+  // Get the field defaults.
+  $dbxref_id = $items[0]['value'];
+  $db_id = $items[0]['dbxref__db_id'];
+  $accession = $items[0]['dbxref__accession'];
+  $version = $items[0]['dbxref__version'];
+  $description = $items[0]['dbxref__description'];
 
   // 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
@@ -76,8 +67,8 @@ function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $fiel
   $schema = chado_get_schema('dbxref');
   $options = tripal_get_db_select_options();
 
-  $widget['#element_validate'] = array('tripal_fields_primary_dbxref_widget_validate');
-  $widget['#theme'] = 'tripal_fields_primary_dbxref_widget';
+  $widget['#element_validate'] = array('tripal_fields_dbxref_id_widget_validate');
+  $widget['#theme'] = 'tripal_fields_dbxref_id_widget';
   $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id'>";
   $widget['#suffix'] =  "</span>";
 
@@ -86,7 +77,7 @@ function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $fiel
     '#title' => $element['#title'],
     '#description' =>  $element['#description'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
-    '#theme' => 'tripal_fields_primary_dbxref_widget',
+    '#theme' => 'tripal_fields_dbxref_id_widget',
     '#collapsible' => TRUE,
     '#collapsed' => $collapsed,
   );
@@ -103,7 +94,7 @@ function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $fiel
     '#required' => $element['#required'],
     '#default_value' => $db_id,
     '#ajax' => array(
-      'callback' => "tripal_fields_primary_dbxref_widget_form_ajax_callback",
+      'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
       'wrapper' => "$field_name-dbxref--db-id",
       'effect' => 'fade',
       'method' => 'replace'
@@ -118,7 +109,7 @@ function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $fiel
     '#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",
+      'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
       'wrapper' => "$field_name-dbxref--db-id",
       'effect' => 'fade',
       'method' => 'replace'
@@ -145,7 +136,7 @@ function tripal_fields_primary_dbxref_widget(&$widget, $form, $form_state, $fiel
 /**
  * An Ajax callback for the tripal_fields_admin_publish_form..
  */
-function tripal_fields_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
+function tripal_fields_dbxref_id_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, 0, 'dbxref__db_id');
   $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
@@ -167,7 +158,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) {
+function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // If the form ID is field_ui_field_edit_form, then the user is editing the
@@ -217,11 +208,11 @@ function tripal_fields_primary_dbxref_widget_validate($element, &$form_state) {
   }
 }
 /**
- * Theme function for the primary_dbxref_widget.
+ * Theme function for the dbxref_id_widget.
  *
  * @param $variables
  */
-function theme_tripal_fields_primary_dbxref_widget($variables) {
+function theme_tripal_fields_dbxref_id_widget($variables) {
   $element = $variables['element'];
 
   $layout = "
@@ -257,4 +248,40 @@ function theme_tripal_fields_primary_dbxref_widget($variables) {
   );
 
   return theme('fieldset', array('element' => $fieldset));
-}
+}
+
+/**
+ * Loads the field values with appropriate data.
+ *
+ * This function is called by the tripal_fields_field_storage_load() for
+ * each property managed by the field_chado_storage storage type.
+ *
+ * @param $field
+ * @param $entity
+ * @param $record
+ */
+function tripal_fields_dbxref_id_field_load($field, $entity, $record) {
+  $field_name = $field['field_name'];
+  $field_type = $field['type'];
+  $field_table = $field['settings']['chado_table'];
+  $field_column = $field['settings']['chado_column'];
+
+
+  // Get the primary dbxref record.  Because we have a dbxref_id passed in
+  // by the base record, we will only have one record.
+  $columns = array('*');
+  $match = array('dbxref_id' => $record->$field_column);
+  $options = array('return_array' => TRUE);
+  $records = chado_select_record('dbxref', $columns, $match, $options);
+  if (count($records) > 0) {
+    $dbxref = $records[0];
+    $entity->{$field_name}['und'][0] = array(
+      'value' => $dbxref->dbxref_id,
+      'dbxref__db_id' => $dbxref->db_id,
+      'dbxref__accession' => $dbxref->accession,
+      'dbxref__version' => $dbxref->version,
+      'dbxref__description' => $dbxref->description,
+    );
+  }
+}
+

+ 4 - 1
tripal_fields/includes/fields/kvproperty_adder.inc

@@ -109,7 +109,10 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
        $field_info = array(
          'field_type' => 'kvproperty',
          'widget_type' => 'tripal_fields_kvproperty_widget',
-         'field_settings' => array(),
+         'field_settings' => array(
+           'chado_table' => $field_name,
+           'chado_column' => 'type_id',
+         ),
          'widget_settings' => array(),
          'description' => $term[0]->definition ? $term[0]->definition : '',
          'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),

+ 17 - 0
tripal_fields/includes/fields/md5checksum.inc

@@ -64,4 +64,21 @@ function tripal_fields_md5checksum_checkbox_widget_validate($element, &$form_sta
     // Otherwise, remove the md5 value
     tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
   }
+}
+
+/**
+ * Loads the field values with appropriate data.
+ *
+ * This function is called by the tripal_fields_field_storage_load() for
+ * each property managed by the field_chado_storage storage type.
+ *
+ * @param $field
+ * @param $entity
+ * @param $record
+ */
+function tripal_fields_md5checksum_field_load($field, $entity, $record) {
+  $field_name = $field['field_name'];
+  if ($record and property_exists($record, 'md5checksum')) {
+    $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);
+  }
 }

+ 2 - 1
tripal_fields/includes/fields/organism_id.inc

@@ -36,7 +36,6 @@ function tripal_fields_organism_select_formatter(&$element, $entity_type, $entit
  * @param $element
  */
 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',
@@ -72,3 +71,5 @@ function tripal_fields_organism_select_widget_validate($element, &$form_state) {
     form_error($element, t("Please specify an organism."));
   }
 }
+
+

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

@@ -247,7 +247,9 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
  * Responsible for loading the fields from the Chado database and adding
  * their values to the entity.
  */
-function tripal_fields_field_storage_load($entity_type, $entities, $age, $fields, $options) {
+function tripal_fields_field_storage_load($entity_type, $entities, $age,
+    $fields, $options) {
+
   $load_current = $age == FIELD_LOAD_CURRENT;
   global $language;
   $langcode = $language->language;
@@ -265,50 +267,77 @@ function tripal_fields_field_storage_load($entity_type, $entities, $age, $fields
       // TODO: what to do if record is missing!
     }
 
-    // Find out which table should receive the insert.
-    $tablename = $details->data_table;
+    // Get some values needed for loading the values from Chado.
+    $base_table = $details->data_table;
     $type_field = $details->field;
-    $schema = chado_get_schema($tablename);
-    $pkey_field = $schema['primary key'][0];
     $record_id = $details->record_id;
 
-    // Iterate through the field names to get the list of tables and fields
-    // that should be queried.
-    $columns = array();
+    // Get this table's schema.
+    $schema = chado_get_schema($base_table);
+    $pkey_field = $schema['primary key'][0];
+
+    // Get the base record if one exists
+    $columns = array('*');
+    $match = array($pkey_field => $record_id);
+    $record = chado_select_record($base_table, $columns, $match);
+    $record = $record[0];
+
+    // Iterate through the entity's fields so we can get the column names
+    // that need to be selected from each of the tables represented.
+    $tables = array();
     foreach ($fields as $field_id => $ids) {
+
       // By the time this hook runs, the relevant field definitions have been
       // populated and cached in FieldInfo, so calling field_info_field_by_id()
       // on each field individually is more efficient than loading all fields in
       // memory upfront with field_info_field_by_ids().
       $field = field_info_field_by_id($field_id);
       $field_name = $field['field_name'];
+      $field_type = $field['type'];
+      $field_module = $field['module'];
 
-      $matches = array();
-      if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
-        $table = $matches[1];
-        $field = $matches[2];
-        $columns[$table][] = $field;
+      // Skip fields that don't map to a Chado table (e.g. kvproperty_adder).
+      if (!array_key_exists('settings', $field) or !array_key_exists('chado_table', $field['settings'])) {
+        continue;
       }
-    }
 
-    // Get the record
-    $values = array($pkey_field => $record_id);
-    $record = chado_select_record($tablename, $columns[$tablename], $values);
+      // Get the Chado table and column for this field.
+      $field_table = $field['settings']['chado_table'];
+      $field_column = $field['settings']['chado_column'];
 
-    // Now set the field values
-    foreach ($fields as $field_id => $ids) {
-      $field = field_info_field_by_id($field_id);
-      $field_name = $field['field_name'];
-      $matches = array();
-      if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
-        $table = $matches[1];
-        $field = $matches[2];
-        $entity->{$field_name}['und'][] = array('value' => $record[0]->$field);
+      // There are only two types of fields: 1) fields that represent a single
+      // column of the base table, or 2) fields that represent a linked record
+      // in a many-to-one relationship with the base table.
+
+      // Type 1: fields from base tables.
+      if ($field_table == $base_table) {
+        if ($record and property_exists($record, $field_column)) {
+          $entity->{$field_name}['und'][0]['value'] = $record->$field_column;
+        }
+        // Allow the creating module to alter the value if desired.  The
+        // module should do this if the field has any other form elements
+        // that need populationg besides the default value.
+        $load_function = $field_module . '_' . $field_type . '_field_load';
+        module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
+        if (function_exists($load_function)) {
+          $load_function($field, $entity, $record);
+        }
       }
-    }
-  }
-}
 
+      // Type 2: fields for linked records.  These fields will have any number
+      // of form elements that might need populating so we'll offload the
+      // loading of these fields to the field itself.
+      if ($field_table != $base_table) {
+        $load_function = $field_module . '_' . $field_type . '_field_load';
+        module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
+        if (function_exists($load_function)) {
+          $load_function($field, $entity, $record);
+        }
+      }
+
+    } // end: foreach ($fields as $field_id => $ids) {
+  } // end: foreach ($entities as $id => $entity) {
+}
 /**
  * Iterates through all of the fields reformats to a key/value array.
  *

+ 15 - 14
tripal_fields/tripal_fields.module

@@ -23,8 +23,8 @@ function tripal_fields_field_info() {
     'dbxref_id' => array(
       'label' => t('Cross-reference'),
       'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference.  At a minimum, the database and accession must be provided.'),
-      'default_widget' => 'tripal_fields_primary_dbxref_widget',
-      'default_formatter' => 'tripal_fields_primary_dbxref_formatter',
+      'default_widget' => 'tripal_fields_dbxref_id_widget',
+      'default_formatter' => 'tripal_fields_dbxref_id_formatter',
       'settings' => array(),
       'storage' => array(
         'type' => 'field_chado_storage',
@@ -111,7 +111,7 @@ function tripal_fields_field_widget_info() {
       'label' => t('Organism Select'),
       'field types' => array('organism_id')
     ),
-    'tripal_fields_primary_dbxref_widget' => array(
+    'tripal_fields_dbxref_id_widget' => array(
       'label' => t('Cross-reference'),
       'field types' => array('dbxref_id'),
       'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference.  At a minimum, the database and accession must be provided.'),
@@ -147,7 +147,7 @@ function tripal_fields_field_formatter_info() {
       'label' => t('Organism'),
       'field types' => array('organism_id')
     ),
-    'tripal_fields_primary_dbxref_formatter' => array(
+    'tripal_fields_dbxref_id_formatter' => array(
       'label' => t('Cross-reference'),
       'field types' => array('dbxref_id')
     ),
@@ -173,6 +173,7 @@ function tripal_fields_field_formatter_info() {
     ),
   );
 }
+
 /**
  * Implements hook_field_formatter_view().
  *
@@ -195,9 +196,9 @@ function tripal_fields_field_formatter_view($entity_type, $entity, $field,
           tripal_fields_organism_select_formatter($element, $entity_type, $entity, $field,
               $instance, $langcode, $items, $display);
           break;
-        case 'tripal_fields_primary_dbxref_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/primary_dbxref');
-          tripal_fields_primary_dbxref_formatter($element, $entity_type, $entity, $field,
+        case 'tripal_fields_dbxref_id_formatter':
+          module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
+          tripal_fields_dbxref_id_formatter($element, $entity_type, $entity, $field,
               $instance, $langcode, $items, $display);
           break;
         case 'tripal_fields_md5checksum_formatter':
@@ -244,10 +245,10 @@ function tripal_fields_field_widget_form(&$form, &$form_state, $field,
       // Update the widget with the new field.
       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');
-      tripal_fields_primary_dbxref_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+    case 'tripal_fields_dbxref_id_widget':
+      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/dbxref_id');
+      module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
+      tripal_fields_dbxref_id_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');
@@ -363,13 +364,13 @@ function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalu
  */
 function tripal_fields_theme($existing, $type, $theme, $path) {
   return array(
-    'tripal_fields_primary_dbxref_widget' => array(
+    'tripal_fields_dbxref_id_widget' => array(
       'render element' => 'element',
-      'file' => 'includes/fields/primary_dbxref.inc',
+      'file' => 'includes/fields/dbxref_id.inc',
     ),
     'tripal_fields_kvproperty_addr_widget' => array(
       'render element' => 'element',
-      'file' => 'includes/fields/primary_dbxref.inc',
+      'file' => 'includes/fields/dbxref_id.inc',
     ),
   );
 }