Browse Source

Converted transcript field to a Class. Changed value for field. Updated formatter as well

Stephen Ficklin 9 years ago
parent
commit
337602ff23

+ 25 - 4
tripal/includes/TripalField.inc

@@ -14,9 +14,9 @@
  * appropriate TripalField class.
  *
  * Because of the way Drupal handles callbacks, form validate functions,
- * AJAX callbacks and theme functions cannot be part of the implementation of
- * this class.  Those functions should be added to the bottom of the file
- * where the child class is housed..
+ * AJAX callbacks, validate functions and theme functions cannot be part of
+ * the implementation of this class.  Those functions should be added to
+ * the bottom of the file where the child class is housed.
  *
  */
 class TripalField {
@@ -114,7 +114,12 @@ class TripalField {
    *  Provides the display for a field
    *
    *  This function provides the display for a field when it is viewed on
-   *  the web page.
+   *  the web page.  The content returned by the formatter should only include
+   *  what is present in the $items[$delta]['values] array. This way, the
+   *  contents that are displayed on the page, via webservices and downloaded
+   *  into a CSV file will always be identical.  It should also include all of
+   *  the data in the $items[$delta]['values] array so that all the data is
+   *  present everywhere.
    *
    *  @param $element
    *  @param $entity_type
@@ -193,6 +198,22 @@ class TripalField {
    * @param $base_table
    * @param $record
    *
+   * @return
+   *   An array of the following format:
+   *     $entity->{$field_name}['und'][0]['value'] = $value;
+   *   where:
+   *     - $entity is the enity object to which this field is attached.
+   *     - $field_name is the name of this field
+   *     - 'und' is the language code (in this case 'und' == undefined)
+   *     - 0 is the cardinality.  Increment by 1 when more than one item is
+   *       available.
+   *     - 'value' is the key indicating the value of this field. It should
+   *       always be set.  The value of the 'value' key will be the contents
+   *       used for web services and for downloadable content.
+   *   The array may contain as many other keys at the same level as 'value'
+   *   but those keys are for internal field use and are not considered the
+   *   value of the field.
+   *
    */
   public function load($field, $entity, $details) {
 

+ 174 - 216
tripal_chado/includes/fields/chado_gene__transcripts.inc

@@ -1,184 +1,133 @@
 <?php
 
-/**
- * Implements hook_info() for fields.
- *
- * This is a hook provided by the tripal_chado module for offloading the
- * hook_field_info() hook for each field to specify.
- */
-function chado_gene__transcripts_info() {
-  return array(
-    'label' => t('Transcripts'),
-    'description' => t('Transcripts of genes.'),
-    'default_widget' => 'chado_gene__transcripts_widget',
-    'default_formatter' => 'chado_gene__transcripts_formatter',
-    'settings' => array(),
-    'storage' => array(
-      'type' => 'field_chado_storage',
-      'module' => 'tripal_chado',
-      'active' => TRUE
-    ),
-  );
-}
-/**
- * Implements hook_attach_info().
- *
- * This is a hook provided by the tripal_Chado module. It allows the field
- * to specify which bundles it will attach to and to specify thee settings.
- *
- * @param $entity_type
- * @param $entity
- * @param $term
- *
- * @return
- *   A field array
- */
-function chado_gene__transcripts_attach_info($entity_type, $bundle, $target) {
-  $field_info = array();
-
-  $table_name = $target['data_table'];
-  $type_table = $target['type_table'];
-  $type_field = $target['field'];
-  $cv_id      = $target['cv_id'];
-  $cvterm_id  = $target['cvterm_id'];
-
-  // If the linker table does not exists or this is not a gene then we don't want to add attach.
-  $rel_table = $table_name . '_relationship';
-  if (!chado_table_exists($rel_table) || $bundle->label != 'gene') {
+class chado_gene__transcripts extends TripalField {
+
+  /**
+   * @see TripalField::field_info()
+   */
+  public function field_info() {
+    return array(
+      'label' => t('Transcripts'),
+      'description' => t('Transcripts of genes.'),
+      'default_widget' => 'chado_gene__transcripts_widget',
+      'default_formatter' => 'chado_gene__transcripts_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    );
+  }
+
+  /**
+   * @see TripalField::attach_info()
+   */
+  public function attach_info($entity_type, $bundle, $target) {
+    $field_info = array();
+
+    $table_name = $target['data_table'];
+    $type_table = $target['type_table'];
+    $type_field = $target['field'];
+    $cv_id      = $target['cv_id'];
+    $cvterm_id  = $target['cvterm_id'];
+
+    // If the linker table does not exists or this is not a gene then we don't want to add attach.
+    $rel_table = $table_name . '_relationship';
+    if (!chado_table_exists($rel_table) || $bundle->label != 'gene') {
+      return $field_info;
+    }
+
+    $schema = chado_get_schema($rel_table);
+    $pkey = $schema['primary key'][0];
+
+    // Initialize the field array.
+    $field_info = array(
+      'field_name' => 'gene__transcripts',
+      'field_type' => 'chado_gene__transcripts',
+      'widget_type' => 'chado_gene__transcripts_widget',
+      'widget_settings' => array('display_label' => 1),
+      'description' => '',
+      'label' => 'Transcripts',
+      'is_required' => 0,
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'storage' => 'field_chado_storage',
+      'field_settings' => array(
+        'chado_table' => $rel_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => array(
+          'type' => '',
+          'ns' => '',
+          'nsurl' => '',
+        ),
+      ),
+    );
+
     return $field_info;
   }
 
-  $schema = chado_get_schema($rel_table);
-  $pkey = $schema['primary key'][0];
-
-  // Initialize the field array.
-  $field_info = array(
-    'field_name' => 'gene__transcripts',
-    'field_type' => 'chado_gene__transcripts',
-    'widget_type' => 'chado_gene__transcripts_widget',
-    'widget_settings' => array('display_label' => 1),
-    'description' => '',
-    'label' => 'Transcripts',
-    'is_required' => 0,
-    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
-    'storage' => 'field_chado_storage',
-    'field_settings' => array(
-      'chado_table' => $rel_table,
-      'chado_column' => $pkey,
-      'base_table' => $table_name,
-      'semantic_web' => array(
-        'type' => '',
-        'ns' => '',
-        'nsurl' => '',
+  /**
+   * @see TripalField::widget_info()
+   */
+  public function widget_info() {
+    return array(
+      'label' => t('Transcripts Settings'),
+      'field types' => array('chado_gene__transcripts')
+    );
+  }
+
+  /**
+   * @see TripalField::formatter_info()
+   */
+  public function formatter_info() {
+    return array(
+      'label' => t('Transcripts'),
+      'field types' => array('chado_gene__transcripts'),
+      'settings' => array(
       ),
-    ),
-  );
-
-  return $field_info;
-
-}
-/**
- * Implements hook_widget_info.
- *
- * This is a hook provided by the tripal_chado module for offloading
- * the hook_field_widget_info() hook for each field to specify.
- */
-function chado_gene__transcripts_widget_info() {
-  return array(
-    'label' => t('Transcripts Settings'),
-    'field types' => array('chado_gene__transcripts')
-  );
-}
-/**
- * Implements hook_formatter_info.
- *
- * This is a hook provided by the tripal_chado module for
- * offloading the hook_field_formatter_info() for each field
- * to specify.
- *
- */
-function chado_gene__transcripts_formatter_info() {
-  return array(
-    'label' => t('Transcripts'),
-    'field types' => array('chado_gene__transcripts'),
-    'settings' => array(
-    ),
-  );
-}
-
-/**
- * Implements hook_formatter_settings_summary.
- *
- * This is a hook provided by the tripal_chado module for
- * offloading the hook_field_formatter_settings_summary() for each field
- * to specify.
- *
- */
-function chado_gene__transcripts_formatter_settings_summary($field, $instance,
-    $view_mode) {
-
-}
-
-/**
- * Provides a settings form for the formatter.
- *
- * This is a hook provided by the tripal_chado module for
- * offloading the hook_field_formatter_settings_form() for each field
- * to specify.
- */
-function chado_gene__transcripts_formatter_settings_form($field, $instance,
-    $view_mode, $form, &$form_state) {
-
-
-
-}
-
-/**
- * Validation function for the chado_gene_featureloc_formatter_settings_form.
- */
-function chado_gene__transcripts_formatter_settings_form_validate(&$form, &$form_state) {
-
-  // Place here as an example for validating the settings form.
-}
-
-/**
- * TODO: because this field is meant to handle any xxxxx_transcripts table
- * then feature hard-coding needs to be replaced as it won't work for
- * stocks, etc.
- */
-function chado_gene__transcripts_formatter(&$element, $entity_type, $entity,
-    $field, $instance, $langcode, $items, $display) {
-
-  // Get the settings
-  $settings = $display['settings'];
-  $record = $entity->chado_record;
-
-  foreach ($items as $delta => $item) {
-
-    $all_transcripts = key_exists('part of', $item['all_transcripts']['object'])  && 
-    key_exists('mRNA', $item['all_transcripts']['object']['part of']) ? 
-    $item['all_transcripts']['object']['part of']['mRNA'] : array();   
-    
-    $headers = array('Feature Name' ,'Unique Name', 'Location');
-    $rows = array();
+    );
+  }
 
-    foreach ($all_transcripts AS $transcript) {
-      // link the feature to it's node
-      $feature_name = $transcript->record->subject_id->name;
-      $entity_id = $transcript->record->subject_id->entity_id;
+  /**
+   * @see TripalField::formatter_settings_summary()
+   */
+  public function formatter_settings_summary($field, $instance,
+      $view_mode) {
+
+  }
+
+  /**
+   * @see TripalField::formatter_settings_form()
+   */
+  public function formatter_settings_form($field, $instance,
+      $view_mode, $form, &$form_state) {
+
+  }
+
+  /**
+   * @see TripalField::formatter_view()
+   */
+  public function formatter_view(&$element, $entity_type, $entity,
+      $field, $instance, $langcode, $items, $display) {
+
+    // Get the settings
+    $settings = $display['settings'];
+    $record = $entity->chado_record;
+
+    $headers = array('Feature Name', 'Unique Name', 'Type', 'Location');
+    $rows = array();
+    foreach ($items as $delta => $item) {
+      $transcript = $item['value'];
+      $feature_name = $transcript['name'];
+      $feature_uname = $transcript['unique name'];
+      $entity_id = $transcript['entity_id'];
       if ($entity_id) {
         $feature_name = l($feature_name, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
       }
-      $locations = $transcript->child_featurelocs;
-      $loc = "";
-      foreach ($locations AS $location) {
-        $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax . "<br>";
-      }
-      $rows[] = array(
-        array('data' => $feature_name, 'width' => '30%'),
-        array('data' => $transcript->record->subject_id->uniquename, 'width' => '30%'),
-        array('data' => $loc, 'width' => '30%'),
-      );
+      $loc = $transcript['location'];
+      $type = $transcript['type'];
+      $rows[] = array($feature_name, $feature_uname, $type, $loc);
     }
     $table = array(
       'header' => $headers,
@@ -192,53 +141,62 @@ function chado_gene__transcripts_formatter(&$element, $entity_type, $entity,
       'colgroups' => array(),
       'empty' => '',
     );
-
     $content = theme_table($table);
+
+    // once we have our table array structure defined, we call Drupal's theme_table()
+    // function to generate the table.
+    $element[$delta] = array(
+      '#type' => 'markup',
+      '#markup' => $content,
+    );
   }
 
-  // once we have our table array structure defined, we call Drupal's theme_table()
-  // function to generate the table.
-  $element[$delta] = array(
-    '#type' => 'markup',
-    '#markup' => $content,
-  ); 
-}
-
-/**
- * Loads the field values with appropriate data.
- *
- * This function is called by the tripal_chado_field_storage_load() for
- * each property managed by the field_chado_storage storage type.  This is
- * an optional hook function that is only needed if the field has
- * multiple form elements.
- */
-function chado_gene__transcripts_load($field, $entity, $base_table, $record) {
-  $field_name = $field['field_name'];
-
-
-  $entity->{$field_name}['und'][0]['all_transcripts'] = tripal_get_feature_relationships($record);
-}
-
-/**
- * Implements hook_ws_formatter().
- */
-// function chado_gene__transcripts_ws_formatter(&$element, $entity_type, $entity,
-//     $field, $instance, $items) {
-
-//   foreach ($items as $delta => $item) {
-
-//   }
-// }
-
-/**
- *  Implements hook_widget().
- */
-function chado_gene__transcripts_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
-
-}
-/**
- * Callback function for validating the chado_gene_featureloc_widget.
- */
-function chado_gene__transcripts_widget_validate($element, &$form_state) {
+  /**
+   * @see TripalField::load()
+   */
+  public function load($field, $entity, $details) {
+    $record = $details['record'];
+
+    $field_name = $field['field_name'];
+
+    // TODO: If the tripal_get_feature_relationships() slows this down then
+    // we may need to write a custom function to get the data.
+    $rels = tripal_get_feature_relationships($record);
 
+    // Set the value to be a array of "table" rows.
+    $transcripts = array();
+    if (key_exists('part of', $rels['object']) &&
+        key_exists('mRNA', $rels['object']['part of'])) {
+     $transcripts =  $rels['object']['part of']['mRNA'];
+    }
+
+    $headers = array('Feature Name' ,'Unique Name', 'Location');
+    $rows = array();
+    $i = 0;
+    foreach ($transcripts as $transcript) {
+      // link the feature to it's node
+      $feature_name = $transcript->record->subject_id->name;
+      $entity_id = $transcript->record->subject_id->entity_id;
+      $locations = $transcript->child_featurelocs;
+      $loc = "";
+      foreach ($locations AS $location) {
+        $loc .= $location->srcfeature_name . ":" . $location->fmin . ".." . $location->fmax;
+      }
+      $entity->{$field_name}['und'][$i]['value'] = array(
+        'name' => $feature_name,
+        'type' => $transcript->record->subject_id->type_id->name,
+        'unique name' => $transcript->record->subject_id->uniquename,
+        'location' => $loc,
+        'entity_id' => $entity_id,
+        'entity_type' => 'TripalEntity',
+      );
+      $i++;
+    }
+  }
+  /**
+   * @see TripalField::widget_form()
+   */
+  public function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+
+  }
 }

+ 27 - 1
tripal_chado/includes/tripal_chado.fields.inc

@@ -662,7 +662,7 @@ function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
   tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
 
 
-  // Call the hook_attach_info() for all Chado fields to see if any of them
+/*   // Call the hook_attach_info() for all Chado fields to see if any of them
   // want to attach themsevles to this bundle.
   // Iterate through the fields, include the file and run the info function.
   $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
@@ -680,6 +680,32 @@ function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
       $field_name = $field_info['field_name'];
       tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
     }
+  } */
+
+  // Find all of the files in the tripal_chado/includes/fields directory.
+  $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
+  $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
+
+  // Iterate through the fields, include the file and run the info function.
+  foreach ($field_files as $file) {
+    $field_type = $file->name;
+    module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
+
+    if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
+      $field_obj = new $field_type();
+      $field_info = $field_obj->attach_info($entity_type, $bundle, $bundle_data);
+    }
+
+    $function = $field_type . '_attach_info';
+    if (function_exists($function)) {
+      $field_info = $function($entity_type, $bundle, $bundle_data);
+    }
+
+    if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
+      continue;
+    }
+    $field_name = $field_info['field_name'];
+    tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
   }
 
   // Adds any remaining base fields that may not have been dealt with