Browse Source

Fixed bug in web services for fields with 'values' array of subvalues

Stephen Ficklin 8 years ago
parent
commit
2492e9bc6e
2 changed files with 106 additions and 39 deletions
  1. 49 0
      tripal_ws/api/tripal_ws.api.inc
  2. 57 39
      tripal_ws/includes/tripal_ws.rest_v0.1.inc

+ 49 - 0
tripal_ws/api/tripal_ws.api.inc

@@ -0,0 +1,49 @@
+<?php
+/**
+ * @file
+ *
+ * This file provides the Tripal Web Services API: a set of functions for
+ * interacting with the Tripal Web Services.
+ */
+
+/**
+ * @defgroup tripal_ws_api Tripal Web Services
+ *
+ * @ingroup tripal_api
+ * The Tripal Web Services API provides a set of functions for interacting
+ * with the Tripal Web Services.
+ *
+ */
+
+/**
+ * Adjust the values of a field for display in web services.
+ *
+ * This hook should be used sparingly. It is meant primarily to adjust 3rd
+ * Party (non Tripal) fields so that they work with web
+ * services.  The caller should adjust the $items array as needed.
+ * This change only affects the value displayed in web services.  Web services
+ * expect that every field have a 'value' element for each of the items. If a
+ * field for some reason does not have a 'value' element then this hook will
+ * allow setting of that element.
+ *
+ * @param $items
+ *   The list of items for the field.
+ * @param $field
+ *   The field array.
+ * @param $instance
+ *   The field instance array.
+ *
+ * @ingroup tripal_ws_api
+ */
+
+function hook_tripal_ws_value(&$items, $field, $instance) {
+  // The image module doesn't properly set the 'value' field, so we'll do it
+  // here.
+  if($field['type'] == 'image' and $field['module'] == 'image') {
+    foreach ($items as $delta => $details) {
+      if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
+        $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
+      }
+    }
+  }
+}

+ 57 - 39
tripal_ws/includes/tripal_ws.rest_v0.1.inc

@@ -793,8 +793,8 @@ function tripal_ws_services_v0_1_write_context(&$response, $ctype) {
 }
 
 /**
-*
-*/
+ *
+ */
 function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $instance, $api_url, &$response, $is_field_page = NULL) {
   // Get the field  settings.
   $field_name = $field['field_name'];
@@ -813,43 +813,10 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
 
   $values = array();
   for ($i = 0; $i < count($items); $i++) {
-
-    // If the value is an array rather than a scalar then map the sub elements
-    // to controlled vocabulary terms.
-    if (is_array($items[$i]['value'])) {
-      $temp = array();
-      foreach ($items[$i]['value'] as $k => $v) {
-        $matches = array();
-        if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
-          $vocabulary = $matches[1];
-          $accession = $matches[2];
-          $term = tripal_get_term_details($vocabulary, $accession);
-          $key_adj = strtolower(preg_replace('/ /', '_', $term['name']));
-          $temp[$key_adj] = $v !== "" ? $v : NULL;
-          // The term schema:url also points to a recource so we need
-          // to make sure we set the type to be '@id'.
-          if ($vocabulary == 'schema' and $accession == 'url') {
-            $response['@context'][$key_adj] = array(
-              '@id' => $term['url'],
-              '@type' => '@id',
-            );
-          }
-          else {
-            $response['@context'][$key_adj] = $term['url'];
-          }
-        }
-      }
-      $values[] = $temp;
-    }
-    else {
-      $values[] = $items[$i]['value'] !== "" ? $items[$i]['value'] : NULL;
-    }
-
-    // Recurse through the values array and set the entity elemetns
-    // and add the fields to the context.
-    tripal_ws_services_v0_1_get_content_add_field_context($items[$i], $response, $api_url);
+    $values[$i] = tripal_ws_services_v0_1_rewrite_field_items_keys($items[$i]['value'], $response, $api_url);
   }
 
+  // Add the $values array to the WS response.
   // If we only have one value then set the response with just the value.
   if (count($values) == 1) {
     // If the value is an array and this is the field page then all of those
@@ -892,18 +859,69 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
     }
   }
 }
+/**
+ *
+ */
+function tripal_ws_services_v0_1_rewrite_field_items_keys($value, &$response, $api_url) {
+
+  $new_value = '';
+  // If the value is an array rather than a scalar then map the sub elements
+  // to controlled vocabulary terms.
+  if (is_array($value)) {
+    $temp = array();
+    foreach ($value as $k => $v) {
+      $matches = array();
+      if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
+        $vocabulary = $matches[1];
+        $accession = $matches[2];
+        $term = tripal_get_term_details($vocabulary, $accession);
+        $key_adj = strtolower(preg_replace('/ /', '_', $term['name']));
+        if (is_array($v)) {
+          $temp[$key_adj] = tripal_ws_services_v0_1_rewrite_field_items_keys($v, $response, $api_url);
+        }
+        else {
+          $temp[$key_adj] = $v !== "" ? $v : NULL;
+        }
+        // The term schema:url also points to a recource so we need
+        // to make sure we set the type to be '@id'.
+        if ($vocabulary == 'schema' and $accession == 'url') {
+          $response['@context'][$key_adj] = array(
+            '@id' => $term['url'],
+            '@type' => '@id',
+          );
+        }
+        else {
+          $response['@context'][$key_adj] = $term['url'];
+        }
+      }
+      else {
+        $temp[$k] = $v;
+      }
+    }
+    $new_value = $temp;
 
+    // Recurse through the values array and set the entity elements
+    // and add the fields to the context.
+    tripal_ws_services_v0_1_rewrite_field_items_entity($new_value, $response, $api_url);
+
+  }
+  else {
+    $new_value = $value !== "" ? $value : NULL;
+  }
+
+  return $new_value;
+}
 /**
  *
  */
-function tripal_ws_services_v0_1_get_content_add_field_context(&$items, &$response, $api_url) {
+function tripal_ws_services_v0_1_rewrite_field_items_entity(&$items, &$response, $api_url) {
 
   if (!$items) {
     return;
   }
   foreach ($items as $key => $value) {
     if (is_array($value)) {
-      tripal_ws_services_v0_1_get_content_add_field_context($items[$key], $response, $api_url);
+      tripal_ws_services_v0_1_rewrite_field_items_entity($items[$key], $response, $api_url);
       continue;
     }