Bläddra i källkod

Added new hook to allow fields to format their web services array as needed

Stephen Ficklin 9 år sedan
förälder
incheckning
838e52d75f

+ 21 - 0
tripal_chado/api/tripal_chado.api.inc

@@ -1,2 +1,23 @@
 <?php
 
+/**
+ * Retrieves an entity that matches the given table and record id.
+ *
+ * @param $table
+ *   The name of the Chado table.
+ * @param $record_id
+ *   The record's primary key in the table specified by $table.
+ */
+function tripal_load_chado_entity($table, $record_id) {
+   $entity_id = db_select('chado_entity', 'ce')
+     ->fields('ce', array('entity_id'))
+     ->condition('ce.record_id', $record_id)
+     ->condition('ce.data_table', $table)
+     ->execute()
+     ->fetchField();
+   if ($entity_id) {
+     $entity = entity_load('TripalEntity', array($entity_id));
+     return reset($entity);
+   }
+   return NULL;
+}

+ 46 - 18
tripal_chado/includes/fields/chado_base__organism_id.inc

@@ -2,14 +2,6 @@
 
 /**
  *
- * @param $entity_type
- * @param $entity
- * @param $field
- * @param $instance
- * @param $langcode
- * @param $items
- * @param $display
- * @return string
  */
 function chado_base__organism_id_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
@@ -23,18 +15,53 @@ function chado_base__organism_id_formatter(&$element, $entity_type, $entity, $fi
     );
   }
 }
+
 /**
+ * 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.
  *
- * @param $field_name
- * @param $widget
- * @param $form
- * @param $form_state
  * @param $field
- * @param $instance
- * @param $langcode
- * @param $items
- * @param $delta
- * @param $element
+ * @param $entity
+ * @param $base_table
+ * @param $record
+ */
+function chado_base__organism_id_load($field, $entity, $base_table, $record) {
+  $field_name = $field['field_name'];
+
+  // The organism_id is already set as the value. We need to get it and
+  // see if there is a published entity for this organism. If there is
+  // then we want to add the organism object generated using chado_generate_var
+  // as well as the entity to the record.
+  $organism_id = $entity->{$field_name}['und'][0]['value'];
+  $organism = chado_generate_var('organism', array('organism_id' => $organism_id));
+  $entity->{$field_name}['und'][0]['organism'] = $organism;
+  $org_entity = tripal_load_chado_entity('organism', $organism_id);
+  $entity->{$field_name}['und'][0]['entity'] = $org_entity;
+}
+/**
+ * Implements hook_ws_formatter().
+ */
+function chado_base__organism_id_ws_formatter(&$element, $entity_type, $entity,
+    $field, $instance, $items) {
+
+  foreach ($items as $delta => $item) {
+    $organism = $item['organism'];
+    $entity = $item['entity'];
+    if ($entity) {
+      $element[$delta]['#entity'] = $entity;
+    }
+    $element[$delta]['genus'] = $organism->genus;
+    $element[$delta]['species'] = $organism->species;
+    $element[$delta]['common_name'] = $organism->common_name;
+  }
+}
+
+/**
+ *  Implements hook_widget().
  */
 function chado_base__organism_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
   $options = tripal_get_organism_select_options(FALSE);
@@ -118,7 +145,7 @@ function chado_base__organism_id_formatter_form($field, $instance,
     '#type' => 'textfield',
     '#title' => 'Display Format',
     '#description' => t('Provide a mixture of text and/or tokens for the format.
-        For example: [organism__genus] [organism__species].  When displayed
+        For example: [organism__genus] [organism__species].  When displayed,
         the tokens will be replaced with the actual value.'),
     '#default_value' => '[organism__genus] [organism__species]',
   );
@@ -169,3 +196,4 @@ function chado_base__organism_id_formatter_form($field, $instance,
 }
 
 
+

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

@@ -295,17 +295,14 @@ function tripal_chado_field_formatter_info() {
       'label' => t('Synonyms'),
       'field types' => array('chado_linker__synonym')
     ),
-
     'chado_linker__cvterm_adder_formatter' => array(
       'label' => t('Add an Annotation'),
       'field types' => array('chado_linker__cvterm_adder')
     ),
-
     'chado_linker__prop_adder_formatter' => array(
       'label' => t('Add a Property'),
       'field types' => array('chado_linker__prop_adder')
     ),
-
     'chado_feature__md5checksum_formatter' => array(
       'label' => t('MD5 checksum'),
       'field types' => array('chado_feature__md5checksum')
@@ -327,7 +324,7 @@ function tripal_chado_field_formatter_settings_summary($field, $instance, $view_
 
   $summary = '';
   switch ($field['type']) {
-    case 'organism_id':
+    case 'chado_base__organism_id':
       $summary = 'Organisms can be displayed in vaious ways.';
       break;
     default:

+ 10 - 1
tripal_chado/includes/tripal_chado.term_storage.inc

@@ -133,13 +133,22 @@ function tripal_chado_vocab_select_term_form($form, &$form_state) {
     );
     $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
     $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
+    $num_terms = 0;
     foreach ($terms as $term) {
        $form['terms_list']['term-' . $term->cvterm_id] = array(
          '#type' => 'checkbox',
          '#title' =>  $term->name,
-         '#description' => '<b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '.  ' .
+         '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name .
+             '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '.  ' .
              '<br><b>Definition:</b>  ' . $term->definition,
        );
+       $num_terms++;
+    }
+    if ($num_terms == 0) {
+      $form['terms_list']['none'] = array(
+        '#type' => 'item',
+        '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
+      );
     }
     // Add in the button for the cases of no terms or too many.
     $form['submit_button'] = array(

+ 58 - 15
tripal_ws/includes/tripal_ws.rest.inc

@@ -458,12 +458,12 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
   // Iterate throught the fields and add each value to the response.
   //$response['fields'] = $fields;
   foreach ($fields as $field_name => $field) {
-    $field_value = $entity->$field_name;
+    $field_info = field_info_field($field_name);
+    $items = field_get_items('TripalEntity', $entity, $field_name);
 
     // By default, the label for the key in the output should be the
     // term from the vocabulary that the field is assigned. But in the
-    // case that the field is not assigned a term, we must use the label
-    // assigned by the Drupal admin UI for the content.
+    // case that the field is not assigned a term, we must use the field name.
     $key = $field['field_name'];
     if (array_key_exists('semantic_web', $field['settings'])) {
       if (array_key_exists('type', $field['settings']['semantic_web']) and
@@ -472,7 +472,7 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
       }
     }
 
-    // Get the semantic web settings for this field
+    // Get the semantic web settings for this field.
     $field_type = '';
     if (array_key_exists('semantic_web', $field['settings'])) {
       $field_type = $field['settings']['semantic_web']['type'];
@@ -484,22 +484,65 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
       }
     }
 
-    // TODO: need a way to hide fields.
+    // Skip hidden fields.
+    if ($field['display']['default']['type'] == 'hidden') {
+      continue;
+    }
 
-    // Get the values based on cardinality
-    $items = field_get_items('TripalEntity', $entity, $field_name);
-    $values = '';
-    if (array_key_exists('und', $field_value) and count($field_value['und']) == 1) {
-      $values = $field_value['und'][0]['value'];
+    // We want to allow the field to format itself for web services if it
+    // wants to.  The file can do so if it implements a '_ws_formatter'
+    // function.
+    $function = $field_info['type'] . '_ws_formatter';
+    module_load_include('inc', $field['display']['default']['module'], 'includes/fields/' . $field_info['type']);
+    if (function_exists($function)) {
+      $values = array();
+      $function($values, $entity->type, $entity, $field_info, $field, $items);
+
+      // Iterate through the key/values returned and handle hashed keys
+      $add_vals = array();
+      for ($i = 0; $i < count($values); $i++) {
+        foreach ($values[$i] as $skey => $svalue) {
+          // If the key is '#entity' then this should like to another
+          // services.
+          if ($skey == '#entity') {
+            $sentity = $svalue;
+            $sbundle = tripal_load_bundle_entity(array('name' => $sentity->bundle));
+            $sterm = tripal_load_term_entity(array('term_id' => $sbundle->term_id));
+            $vocab = $sterm->vocab;
+            $add_vals['@id'] = $api_url . '/content/' . urlencode($sterm->name) . '/' . $sentity->id;
+            $add_vals['@type'] = $vocab->namespace . ':' . $sterm->name;
+            unset($values[$i][$skey]);
+          }
+        }
+        $values[$i] = array_merge($add_vals, $values[$i]);
+      }
+
+
+      if (count($values) == 0) {
+        $response[$key] = '';
+      }
+      if (count($values) == 1) {
+        $response[$key] = $values[0];
+      }
+      else {
+        $response[$key] = $values;
+      }
     }
-    // If cardinality is greater than 1 then the value should be an array
+    // If a function doesn't exist then just show the default value is is.
     else {
-      $values = array();
-      for ($i = 0; $i < count($field_value); $i++) {
-        $values[] = $field_value['und'][$i]['value'];
+      // Get the values based on cardinality
+      if (count($items) == 1) {
+        $values = $items[0]['value'];
+      }
+      // If cardinality is greater than 1 then the value should be an array
+      else {
+        $values = array();
+        for ($i = 0; $i < count($items); $i++) {
+          $values[] = $items[$i]['value'];
+        }
       }
+      $response[$key] = $values;
     }
-    $response[$key] = $values;
   }
 
  //$response['fields'] = $fields;