소스 검색

Added function for a drop-down instead of fieldset for the transcripts

Stephen Ficklin 4 년 전
부모
커밋
0719cdf99b

+ 8 - 3
tripal_chado/includes/TripalFields/so__transcript/so__transcript.inc

@@ -201,8 +201,13 @@ class so__transcript extends ChadoField {
       '#default_value' => $settings['transcript_fields'],
     ];
 
-    //Get the content type for the mRNA term (SO:0000234) and it's feilds.
+    // Get the content type for the mRNA term (SO:0000234) and it's feilds.
+    // If it isn't published then don't show the form below.
     $bundle = tripal_load_bundle_entity(['accession' => 'SO:0000234']);
+    if (!$bundle) {
+      return $element;
+    }
+
     $fields = field_info_instances('TripalEntity', $bundle->name);
 
     foreach ($fields as $field_name => $details) {
@@ -324,7 +329,7 @@ class so__transcript extends ChadoField {
       'sticky' => TRUE,
       'caption' => t('Content Panes Available in the TOC'),
       'colgroups' => [],
-      'empty' => t('There are no content panes for this page'),
+      'empty' => t('There are no content panes for this page. The mRNA content type must exist to customize.'),
     ];
     drupal_add_tabledrag('tripal-field-settings-table', 'order', 'sibling', 'tripal-field-settings-table-weights');
 
@@ -332,7 +337,7 @@ class so__transcript extends ChadoField {
     $fset['fields'] = [
       '#type' => 'fieldset',
       '#title' => 'Transcript (mRNA) Field Selection',
-      '#description' => t('Select the fields that you want displayed on the gene page for each transcript.  If no fields are selected then a default display is provided.'),
+      '#description' => t('Here, you can customize what information is provided to the end-user about transcripts.  Select the fields that you want displayed on the page.  If no fields are selected then a default display is provided. All mRNA for a gene must be published or the default display will be provided.'),
       '#collapsible' => TRUE,
       '#collapsed' => TRUE,
       '#attributes' => ['class' => ['collapsible', 'collapsed']],

+ 105 - 18
tripal_chado/includes/TripalFields/so__transcript/so__transcript_formatter.inc

@@ -21,18 +21,39 @@ class so__transcript_formatter extends ChadoFieldFormatter {
       return $this->returnEmpty($element);
     }
 
+    // Get the list of fields that are t o be shown
+    $fields_to_show = [];
+    if (array_key_exists('transcript_fields', $this->instance['settings'])) {
+      $fields_to_show = $this->fieldsToShow($this->instance['settings']['transcript_fields']);
+    }
+
     // For backwards compatibility if the 'transcript_fields' setting
     // is not available then return the default table.
-    if (!array_key_exists('transcript_fields', $this->instance['settings'])) {
+    if (count($fields_to_show) == 0) {
       return $this->returnDefaultTable($element, $items);
     }
 
+    // If any of the transcripts are not published then just provide the
+    // default view
+    foreach ($items as $delta => $item) {
+
+      // Skip empty items;
+      if (!$item['value']) {
+        continue;
+      }
+
+      // If any transcript is not published (i.e. it doesn't have an entity)
+      // then we want to just return the default table and stop.
+      if (!array_key_exists('entity', $item['value'])) {
+        return $this->returnDefaultTable($element, $items);
+      }
+    }
+
     // Iterate through the list of fields so that we can load the mRNA
     // entity with all of the field requested by the site admin. If we
     // don't do this first then auto_attach fields won't be added.
-    $fields = $this->instance['settings']['transcript_fields'];
     $field_ids = [];
-    foreach ($fields as $field_name => $field_value) {
+    foreach ($fields_to_show as $field_name) {
       $field_info = field_info_field($field_name);
       $field_ids[] = $field_info['id'];
     }
@@ -43,7 +64,33 @@ class so__transcript_formatter extends ChadoFieldFormatter {
       return $this->returnDefaultTable($element, $items);
     }
 
+    #return $this->returnFieldsets($items, $element, $field_ids, $fields_to_show);
+    return $this->returnDropDown($items, $element, $field_ids, $fields_to_show);
+
+  }
+
+  /**
+   * Returns a list of fields names that the site admin wants to show.
+   */
+  private function fieldsToShow($transcript_fields) {
+    $to_show = [];
+    foreach($transcript_fields as $field_name => $details) {
+      if ($details['show'] == 1) {
+        $to_show[] = $field_name;
+      }
+    }
+    return $to_show;
+  }
+
+  /**
+   *
+   */
+  private function returnDropDown($items, &$element, $field_ids, $fields_to_show) {
+
+    drupal_add_js(drupal_get_path ('module', 'tripal_chado') . '/theme/js/so__transcript.js');
+
     // Iterate through each mRNA (transcript).
+    $options = [0 => '--Select a transcript to view--'];
     $transcripts = [];
     foreach ($items as $delta => $item) {
 
@@ -52,11 +99,56 @@ class so__transcript_formatter extends ChadoFieldFormatter {
         continue;
       }
 
-      // If any transcript is not published (i.e. it doesn't have an entity)
-      // then we want to just return the default table and stop.
-      if (!array_key_exists('entity', $item['value'])) {
-        return $this->returnDefaultTable($element, $items);
+      list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
+      $result = tripal_load_entity('TripalEntity', [$mRNA_entity_id], FALSE, $field_ids);
+      reset($result);
+      $mRNA_entity = $result[$mRNA_entity_id];
+      $options[$mRNA_entity_id] = $mRNA_entity->title;
+
+      // Create the fieldset for this transcript.
+      $transcripts[$mRNA_entity_id] = [
+        '#type' => 'markup',
+        '#prefix' => '<div id="' . 'tripal-chado-so__transcript-' . $mRNA_entity_id . '" class="tripal-chado-so__transcript-box">',
+        '#suffix' => '</div>',
+      ];
+
+      // Add a link to the mRNA page.
+      $feature_name = $mRNA_entity->title;
+      $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
+      $transcripts[$mRNA_entity_id]['mRNA_link'] = [
+        '#type' => 'markup',
+        '#markup' => "<i>Click, " . $feature_name . ", for the full transcript page.</i>",
+        '#weight' => -102
+      ];
+
+      // Now add all fields to the fieldset.
+      $this->addFields($mRNA_entity, $fields_to_show, $transcripts[$mRNA_entity_id]);
+
+    }
+    $transcripts['transcript_dropdown'] = [
+      '#type' => 'select',
+      '#title' => 'Transcripts',
+      '#options' => $options,
+      '#weight' => -100,
+      '#attributes' => ['class' => ['tripal-chado-so__transcript-select']]
+    ];
+    $element[0] = $transcripts;
+  }
+
+  /**
+   *
+   */
+  private function returnFieldsets($items, &$element, $field_ids, $fields_to_show) {
+
+    // Iterate through each mRNA (transcript).
+    $transcripts = [];
+    foreach ($items as $delta => $item) {
+
+      // Skip empty items;
+      if (!$item['value']) {
+        continue;
       }
+
       list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
 
       // Now load the mRNA entity with all of the fields.
@@ -81,11 +173,11 @@ class so__transcript_formatter extends ChadoFieldFormatter {
       $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
       $transcripts[$mRNA_entity_id]['mRNA_link'] = [
         '#type' => 'markup',
-        '#markup' => "<i>Click, " . $feature_name . ", For the full transcript page.</i>",
+        '#markup' => "<i>Click, " . $feature_name . ", for the full transcript page.</i>",
       ];
 
       // Now add all fields to the fieldset.
-      $this->addFieldsetFields($mRNA_entity, $fields, $transcripts[$mRNA_entity_id]);
+      $this->addFields($mRNA_entity, $fields_to_show, $transcripts[$mRNA_entity_id]);
 
     }
     $element[0] = $transcripts;
@@ -94,7 +186,7 @@ class so__transcript_formatter extends ChadoFieldFormatter {
   /**
    * Builds a fieldset for the entity.
    */
-  private function addFieldsetFields($mRNA_entity, $fields, &$fieldset) {
+  private function addFields($mRNA_entity, $fields, &$fieldset) {
 
     // Get some settings about the transcript content type.
     $mRNA_bundle = tripal_load_bundle_entity(['name'=> $mRNA_entity->bundle]);
@@ -110,14 +202,9 @@ class so__transcript_formatter extends ChadoFieldFormatter {
     // Iterate through the list of fields that the site admin has indicated
     // that they want to show in the transcript fieldset. They are provided
     // in order that they should be shown.
-    foreach ($fields as $field_name => $field_value) {
-
-      // Skip fields that the site admin does not want to show.
-      if (array_key_exists('show', $field_value) and ($field_value['show'] != 1)) {
-        continue;
-      }
+    foreach ($fields as $field_name) {
 
-      // Load the field instance info. We'll need this to determine the
+        // Load the field instance info. We'll need this to determine the
       // cardinality of the field and to render it for display.
       $field_info = field_info_field($field_name);
       $field_instance = field_info_instance('TripalEntity', $field_name, $mRNA_entity->bundle);
@@ -188,7 +275,7 @@ class so__transcript_formatter extends ChadoFieldFormatter {
 
       $fieldset['summary_header'] = [
         '#type' => 'markup',
-        '#markup' => '<h3>Transcript Summary</h3>',
+        '#markup' => '<h3>Transcript ' . $mRNA_entity->title . '</h3>',
         '#weight' => -101,
       ];
       $fieldset['summary_table'] = [

+ 14 - 0
tripal_chado/theme/js/so__transcript.js

@@ -0,0 +1,14 @@
+// Using the closure to map jQuery to $. 
+(function ($) {
+  // Store our function as a property of Drupal.behaviors.
+  Drupal.behaviors.so__transcript = {
+    attach: function (context, settings) {
+
+      $(".tripal-chado-so__transcript-box").hide()
+      $(".tripal-chado-so__transcript-select").change(function() {
+        $(".tripal-chado-so__transcript-box").hide()
+        $("#tripal-chado-so__transcript-" + this.value).show(600)
+      });
+    }
+  }
+}) (jQuery);