Browse Source

moved functions for working with fields that were in the tripal_entity module into the tripal_fields module where they should have been

Stephen Ficklin 9 years ago
parent
commit
755cb05fff

+ 10 - 21
tripal_entities/includes/tripal_entities.admin.inc

@@ -46,14 +46,14 @@ function tripal_entities_content_view() {
  *
  * @param array $form
  * @param array $form_state
- * @return 
+ * @return
  *   A form array describing this listing to the Form API.
  */
 function tripal_entities_content_overview_form($form, &$form_state) {
 
   // Set the title to be informative (defaults to content for some reason).
   drupal_set_title('Biological Data');
-  
+
   // Retrieve a pages list of all tripal entitles (ie: biological data).
   // This will return the 25 most recently created entities.
   $entities = db_select('tripal_entity', 'td')
@@ -64,16 +64,16 @@ function tripal_entities_content_overview_form($form, &$form_state) {
 
   $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
   $rows = array();
-  
+
   // For each entity retrieved add a row to the data listing.
   while ($entity = $entities->fetchObject()) {
-  
+
     // Retrieve details about the term this entity is based on.
     $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $entity->cvterm_id));
-    
+
     // Retrieve details about the user who created this data.
     $author = user_load($entity->uid);
-    
+
     // Add information to the table.
     $rows[] = array(
       l($entity->title, 'BioData/' . $entity->id),
@@ -86,7 +86,7 @@ function tripal_entities_content_overview_form($form, &$form_state) {
       l('delete', 'BioData/' . $entity->id . '/delete')
     );
   }
-  
+
   // If there are no entites created yet then add a message to the table to
   // provide guidance to administrators.
   if (empty($rows)) {
@@ -112,7 +112,7 @@ function tripal_entities_content_overview_form($form, &$form_state) {
     '#type' => 'markup',
     '#markup' => theme('table', $table_vars),
   );
-  
+
   return $form;
 }
 
@@ -200,7 +200,7 @@ function tripal_entities_admin_bundles_form_submit($form, $form_state) {
  *
  * @param array $form
  * @param array $form_state
- * @return 
+ * @return
  *    An array describing this form to the Form API.
  */
 function tripal_entities_admin_add_type_form($form, &$form_state) {
@@ -711,18 +711,7 @@ function tripal_entities_add_term_usage($cvterm, &$form_state) {
 
   return TRUE;
 }
-/**
- * Implements hook_chado_field_alter.
- *
- * This function is used when new Chado fields are addd to an Entity.  It
- * allows modules to customize the field, widget types and settings for
- * a field before it is created.
- *
- * @param $field
- */
-function hook_chado_field_alter(&$field) {
-  // TODO: add example code for how to use this hook.
-}
+
 /**
  *
  * @param unknown $form

+ 0 - 92
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -65,39 +65,6 @@ function tripal_entities_entity_delete($entity, $type) {
   }
 }
 
-/**
- * @param unknown $display
- * @param unknown $context
- */
-function tripal_entities_field_widget_form_alter(&$element, &$form_state, $context) {
-
-  if (array_key_exists('#field_name', $element)) {
-    $field_name = $element['#field_name'];
-    $matches = array();
-
-    if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
-      $tablename = $matches[1];
-      $colname = $matches[2];
-      $schema = chado_get_schema($tablename);
-
-      // The timelastmodified field exists in many Chado tables.  We want
-      // the form element to update to the most recent time rather than the time
-      // in the database.
-      if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
-        // We want the default value for the field to be the current time.
-        $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
-        $element['#date_items']['value'] = $element['#default_value']['value'];
-      }
-      // We want the date combo fieldset to be collaspible so we will
-      // add our own theme_wrapper to replace the one added by the date
-      // module.
-      if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
-        $element['#theme_wrappers'] = array('tripal_entities_date_combo');
-      }
-    }
-  }
-}
-
 /**
  * This theme function is meant to override the data_combo theme.
  *
@@ -119,66 +86,7 @@ function theme_tripal_entities_date_combo($variables) {
   );
   return theme('fieldset', array('element' => $fieldset));
 }
-/**
- * Implements hook_chado_field_alter().
- *
- */
-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['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['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['field_settings']['chado_column'] =='dbxref_id') {
-    $field['field_type'] = 'dbxref_id';
-    $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
-    $field['label'] = 'Primary Cross Reference';;
-    $field['description'] = 'This record can be cross-referenced with a
-        record in another online database. The primary reference is for the
-        most prominent reference.  At a minimum, the database and accession
-        must be provided.  To remove a set reference, change the database
-        field to "Select a Database".';
-  }
-  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['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['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';
-    $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
-  }
-  else if ($field['label'] == 'Timeaccessioned') {
-    $field['label'] = 'Time Accessioned';
-    $field['description'] = 'Please enter the time that this record was first added to the database.';
-  }
-  else if ($field['label'] == 'Timelastmodified') {
-    $field['label'] = 'Time Last Modified';
-    $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
-  }
-}
 
 /**
  * Determines whether the given user has access to a tripal data entity.

+ 114 - 7
tripal_fields/tripal_fields.module

@@ -178,15 +178,122 @@ function tripal_fields_field_formatter_info() {
 }
 
 /**
- * Implements hook_field_formatter_view().
+ * Implements hook_chado_field_alter().
+ *
+ * This function is used to change the default field formatter and widget
+ * that are assigned to fields of an Entity. This hook is only used for
+ * those fields that correspond to a column in a Chado table.  An implementation
+ * of this hook can be used to change the default formatters and widgets to
+ * custom formatters and widgets that are created by the module creating
+ * this hook.
  *
- * Two formatters are implemented.
- * - field_example_simple_text just outputs markup indicating the color that
- *   was entered and uses an inline style to set the text color to that value.
- * - field_example_color_background does the same but also changes the
- *   background color of div.region-content.
+ * By default, Tripal will provide custom formatters and widgets for many
+ * columns in Chado tables, therefore, this hook will most likely be of use
+ * to extension modules that create custom table inside of Chado.
  *
- * @see field_example_field_formatter_info()
+ * @param $field
+ */
+function hook_chado_field_alter(&$field) {
+  // TODO: add example code for how to use this hook.
+}
+
+/**
+ * Implements hook_chado_field_alter().
+ *
+ * This function adds the custom formatters and widgets to many of the Chado
+ * tables.  This way Tripal users get a nice set of already usable fields.
+ */
+function tripal_fields_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['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['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['field_settings']['chado_column'] =='dbxref_id') {
+    $field['field_type'] = 'dbxref_id';
+    $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
+    $field['label'] = 'Primary Cross Reference';;
+    $field['description'] = 'This record can be cross-referenced with a
+    record in another online database. The primary reference is for the
+    most prominent reference.  At a minimum, the database and accession
+    must be provided.  To remove a set reference, change the database
+    field to "Select a Database".';
+  }
+  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['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['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';
+    $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
+  }
+  else if ($field['label'] == 'Timeaccessioned') {
+    $field['label'] = 'Time Accessioned';
+    $field['description'] = 'Please enter the time that this record was first added to the database.';
+  }
+  else if ($field['label'] == 'Timelastmodified') {
+    $field['label'] = 'Time Last Modified';
+    $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
+  }
+}
+
+/**
+ * Implements hook_field_widget_form_alter().
+ */
+function tripal_fields_field_widget_form_alter(&$element, &$form_state, $context) {
+
+  if (array_key_exists('#field_name', $element)) {
+    $field_name = $element['#field_name'];
+    $matches = array();
+
+    if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
+      $tablename = $matches[1];
+      $colname = $matches[2];
+      $schema = chado_get_schema($tablename);
+
+      // The timelastmodified field exists in many Chado tables.  We want
+      // the form element to update to the most recent time rather than the time
+      // in the database.
+      if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
+        // We want the default value for the field to be the current time.
+        $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
+        $element['#date_items']['value'] = $element['#default_value']['value'];
+      }
+      // We want the date combo fieldset to be collaspible so we will
+      // add our own theme_wrapper to replace the one added by the date
+      // module.
+      if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
+        $element['#theme_wrappers'] = array('tripal_entities_date_combo');
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_field_formatter_view().
  */
 function tripal_fields_field_formatter_view($entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {