Kaynağa Gözat

Fixed bugs with autoloading

Stephen Ficklin 8 yıl önce
ebeveyn
işleme
cfcebbb937

+ 7 - 17
tripal/includes/TripalEntityController.inc

@@ -489,6 +489,7 @@ class TripalEntityController extends EntityAPIController {
     $ct = field_info_field('content_type');
     $ids[] = $ct['id'];
     $field_ids += $ids;
+    $unattached = array();
 
 
     // Attach fields.
@@ -515,27 +516,15 @@ class TripalEntityController extends EntityAPIController {
                 else {
                   $instance = $instances[$field_name . '-' . $bundle_name];
                 }
-                // If the 'auto_attach' is set then add this field id to our
-                // list.
+                // If the 'auto_attach' is set to FALSE then we don't want
+                // to add this field to our list of IDs to auto attach.
                 if (array_key_exists('settings', $instance) and
                     array_key_exists('auto_attach', $instance['settings']) and
-                    $instance['settings']['auto_attach'] === TRUE) {
-                  $field_ids[] = $field['id'];
+                    $instance['settings']['auto_attach'] == FALSE) {
+                  // Skip this field.
                 }
-                // If the field is not auto attached then we want to add an
-                // empty default valuea and add an 'unattached' key to
-                // clue in other things that there may be data, it's just
-                // not attached.
                 else {
-                   $value = array(
-                     'und' => array(
-                       0 => array(
-                         'value' => '',
-                         '#unattached' => TRUE,
-                       ),
-                     ),
-                   );
-                   $entity->$field_name = $value;
+                  $field_ids[] = $field['id'];
                 }
               }
             }
@@ -553,6 +542,7 @@ class TripalEntityController extends EntityAPIController {
       $function = $module . '_entity_load';
       $function($queried_entities, $this->entityType);
     }
+
     // Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
     // always the queried entities, followed by additional arguments set in
     // $this->hookLoadArguments.

+ 6 - 0
tripal/includes/TripalFields/TripalField.inc

@@ -44,6 +44,12 @@ class TripalField {
     // Set to TRUE if the site admin is not allowed to change the term
     // type, otherwise the admin can change the term mapped to a field.
     'term_fixed' => FALSE,
+    // Inidates if this field should be automatically attached to display
+    // or web services or if this field should be loaded separately. This
+    // is convenient for speed.  Fields that are slow should for loading
+    // should ahve auto_attach set to FALSE so tha their values can be
+    // attached asyncronously.
+    'auto_attach' => TRUE,
   );
 
   // The default widget for this field.

+ 3 - 6
tripal/includes/tripal.entity.inc

@@ -298,12 +298,9 @@ function tripal_entity_view($entity, $type, $view_mode, $langcode) {
       // Surround the field with a <div> box for AJAX loading if this
       // field is unattached.  this will allow JS code to automatically load
       // the field.
-      $child = $entity->content[$child_name];
-      if (array_key_exists('#items', $child) and
-          array_key_exists('0', $child['#items']) and
-          is_array($child['#items'][0]) and
-          array_key_exists('#unattached', $child['#items'][0])
-          and $child['#items'][0]['#unattached'] === TRUE) {
+      $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
+      if (array_key_exists('auto_attach', $instance['settings']) and
+          $instance['settings']['auto_attach'] == FALSE) {
         $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" class="tripal-entity-unattached">';
         $entity->content[$child_name]['#suffix'] .= '</div>';
       }

+ 31 - 1
tripal_ws/includes/tripal_ws.rest_v0.1.inc

@@ -627,7 +627,7 @@ function tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_u
     // so that the caller can get the information separately.
     $instance_settings = $instance['settings'];
     if (array_key_exists('auto_attach', $instance['settings']) and
-        !$instance_settings['auto_attach']) {
+        $instance_settings['auto_attach'] == FALSE) {
       $response['@context'][$key_adj] = array(
         '@id' => $response['@context'][$key_adj],
         '@type' => '@id'
@@ -804,6 +804,12 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
     return;
   }
 
+  // Give modules the opportunity to edit values for web services. This hook
+  // really should be used sparingly. Where it helps is with non Tripal fields
+  // that are added to a TripalEntity content type and it doesn't follow
+  // the rules (e.g. Image field).
+  drupal_alter('tripal_ws_value', $items, $field, $instance);
+
   $values = array();
   for ($i = 0; $i < count($items); $i++) {
 
@@ -891,6 +897,9 @@ function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $i
  */
 function tripal_ws_services_v0_1_get_content_add_field_context(&$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);
@@ -996,4 +1005,25 @@ function tripal_ws_services_v0_1_handle_no_service($api_url, &$response) {
   $response['@context']['Service'] = 'dc:Service';
   $response['@context']['label'] = 'rdfs:label';
   $response['@context']['description'] = 'hydra:description';
+}
+
+/**
+ * Implements hook_tripal_ws_value_alter().
+ *
+ * The hook_tripal_ws_value_alter is a hook created by the Tripal WS module.
+ * It allows the modules to 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.
+ */
+function tripal_ws_tripal_ws_value_alter(&$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']);
+      }
+    }
+  }
 }