Browse Source

Fixed issues with chaching. Fields now only load via ajax when fields are not hidden

Stephen Ficklin 7 years ago
parent
commit
4335ebf54a

+ 17 - 0
tripal/api/tripal.fields.api.inc

@@ -48,6 +48,11 @@ function tripal_get_field_types() {
 
   $modules = module_list(TRUE);
   foreach ($modules as $module) {
+
+    // Only run this for modules that are enabled.
+    if (!module_exists($module)) {
+      continue;
+    }
     // Find all of the files in the tripal_chado/includes/TripalFields/ directory.
     $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
     $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
@@ -82,6 +87,12 @@ function tripal_get_field_widgets() {
 
   $modules = module_list(TRUE);
   foreach ($modules as $module) {
+
+    // Only run this for modules that are enabled.
+    if (!module_exists($module)) {
+      continue;
+    }
+
     // Find all of the files in the tripal_chado/includes/fields directory.
     $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
     $field_files = file_scan_directory($fields_path, '/.*_widget\.inc$/');
@@ -113,6 +124,12 @@ function tripal_get_field_formatters() {
 
   $modules = module_list(TRUE);
   foreach ($modules as $module) {
+
+    // Only run this for modules that are enabled.
+    if (!module_exists($module)) {
+      continue;
+    }
+
     // Find all of the files in the tripal_chado/includes/fields directory.
     $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
     $field_files = file_scan_directory($fields_path, '/.*_formatter\.inc$/');

+ 17 - 17
tripal/includes/TripalEntityController.inc

@@ -511,24 +511,27 @@ class TripalEntityController extends EntityAPIController {
           // because the field_attach_load won't do it for us.
           $cfid = "field:TripalEntity:$id:$field_name";
 
+          // Check if the field is cached. if so, then don't reload.
+          if ($field_cache) {
+            $cache_data = cache_get($cfid, 'cache_field');
+            if (!empty($cache_data)) {
+              $queried_entities[$id]->$field_name = $cache_data->data;
+              $queried_entities[$id]->$field_name['#processed'] = TRUE;
+              continue;
+            }
+          }
+
           // If a list of field_ids is provided then we specifically want
           // to only load the  fields specified.
           if (count($field_ids) > 0) {
             if (in_array($field_id, $field_ids)) {
-              // Check if the field is cached. if so, then don't reload.
-              if ($field_cache) {
-                $cache_data = cache_get($cfid, 'cache_field');
-                if (!empty($cache_data)) {
-                  $queried_entities[$id]->$field_name = $cache_data->data;
-                  continue;
-                }
-              }
               $function($this->entityType, array($entity->id => $queried_entities[$id]),
                   FIELD_LOAD_CURRENT, $options);
               // Cache the field.
               if ($field_cache) {
                 cache_set($cfid, $entity->$field_name, 'cache_field');
               }
+              $queried_entities[$id]->$field_name['#processed'] = TRUE;
             }
           }
           // If we don't have a list of fields then load them all, but only
@@ -539,23 +542,20 @@ class TripalEntityController extends EntityAPIController {
             if (array_key_exists('settings', $instance) and
                 array_key_exists('auto_attach', $instance['settings']) and
                 $instance['settings']['auto_attach'] == FALSE) {
-               continue;
+               // Add an empty value. This will allow the tripal_entity_view()
+               // hook to add the necessary prefixes to the field for ajax
+               // loading.
+               $queried_entities[$id]->$field_name['und'][0]['value'] = '';
+               $queried_entities[$id]->$field_name['#processed'] = FALSE;
             }
             else {
-              // Check if the field is cached. if so, then don't reload.
-              if ($field_cache) {
-                $cache_data = cache_get($cfid, 'cache_field');
-                if (!empty($cache_data)) {
-                  $queried_entities[$id]->$field_name = $cache_data->data;
-                  continue;
-                }
-              }
               $function($this->entityType, array($entity->id => $queried_entities[$id]),
                   FIELD_LOAD_CURRENT, $options);
               // Cache the field.
               if ($field_cache) {
                 cache_set($cfid, $entity->$field_name, 'cache_field');
               }
+              $queried_entities[$id]->$field_name['#processed'] = TRUE;
             }
           }
         }

+ 8 - 2
tripal/includes/tripal.entity.inc

@@ -312,6 +312,7 @@ function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type
  * retrieve the field.
  */
 function tripal_entity_view($entity, $type, $view_mode, $langcode) {
+
   if ($type == 'TripalEntity') {
     foreach (element_children($entity->content) as $child_name) {
 
@@ -330,8 +331,13 @@ function tripal_entity_view($entity, $type, $view_mode, $langcode) {
       if ($instance and array_key_exists('settings', $instance)) {
         $class = '';
         if (array_key_exists('auto_attach', $instance['settings']) and
-            $instance['settings']['auto_attach'] == FALSE) {
-          $class = 'class="tripal-entity-unattached"';
+            $instance['settings']['auto_attach'] == FALSE and
+            $entity->$child_name['#processed'] == FALSE) {
+          // If the field is empty then try to use ajax to load it.
+          $items = field_get_items('TripalEntity', $entity, $child_name);
+          if (count($items) == 0 or empty($items[0]['value'])) {
+            $class = 'class="tripal-entity-unattached"';
+          }
         }
         $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
         $entity->content[$child_name]['#suffix'] .= '</div>';

+ 2 - 1
tripal/includes/tripal.fields.inc

@@ -152,7 +152,8 @@ function tripal_field_formatter_info_alter(&$info) {
 /**
  * Implements hook_bundle_create().
  *
- * This is a Triapl defined hook and is called in the TripalBundle::create()
+ * This is a Tripal defined hook and is called in the
+ * TripalBundleController::create()
  * function to allow modules to perform tasks when a bundle is created.
  */
 function tripal_bundle_create($bundle, $args) {

+ 12 - 10
tripal/theme/js/tripal.js

@@ -7,16 +7,18 @@
       $(".tripal-entity-unattached .field-items").replaceWith('<div class="field-items">Loading... <img src="' + tripal_path + '/theme/images/ajax-loader.gif"></div>');
       $(".tripal-entity-unattached").each(function() {
         id = $(this).attr('id');
-        $.ajax({
-          url: baseurl + '/bio_data/ajax/field_attach/' + id,
-          dataType: 'json',
-          type: 'GET',
-          success: function(data){
-            var content = data['content'];
-            var id = data['id'];
-            $("#" + id + ' .field-items').replaceWith(content);
-          }
-        });
+        if (id) {
+          $.ajax({
+            url: baseurl + '/bio_data/ajax/field_attach/' + id,
+            dataType: 'json',
+            type: 'GET',
+            success: function(data){
+              var content = data['content'];
+              var id = data['id'];
+              $("#" + id + ' .field-items').replaceWith(content);
+            }
+          });
+        }
       });
     }
   }

+ 1 - 1
tripal_chado/includes/TripalFields/data__sequence_coordinates/data__sequence_coordinates_formatter.inc

@@ -23,7 +23,7 @@ class data__sequence_coordinates_formatter extends ChadoFieldFormatter {
 
     $content = '';
     foreach ($items as $item) {
-      if (!empty($item)) {
+      if (!empty($item['value'])) {
         $srcfeature = $item['value']['data:3002'];
         $fmin = $item['value']['local:fmin'];
         $fmax = $item['value']['local:fmax'];

+ 11 - 11
tripal_chado/includes/tripal_chado.fields.inc

@@ -1386,7 +1386,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'local__contact_formatter',
           'settings' => array(),
         ),
@@ -1421,7 +1421,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'inline',
+          'label' => 'hidden',
           'type' => 'sbo__database_cross_reference_formatter',
           'settings' => array(),
         ),
@@ -1493,7 +1493,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'data__sequence_coordinates_formatter',
           'settings' => array(),
         ),
@@ -1527,7 +1527,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'ogi__location_on_map_formatter',
           'settings' => array(),
         ),
@@ -1562,7 +1562,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'so__genotype_formatter',
           'settings' => array(),
         ),
@@ -1597,7 +1597,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'sbo__phenotype_formatter',
           'settings' => array(),
         ),
@@ -1667,7 +1667,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
          ),
          'display' => array(
            'default' => array(
-             'label' => 'inline',
+             'label' => 'hidden',
              'type' => 'chado_linker__prop_formatter',
              'settings' => array(),
            ),
@@ -1704,7 +1704,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'schema__publication_formatter',
           'settings' => array(),
         ),
@@ -1740,7 +1740,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'sio__references_formatter',
           'settings' => array(),
         ),
@@ -1776,7 +1776,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'above',
+          'label' => 'hidden',
           'type' => 'sbo__relationship_formatter',
           'settings' => array(),
         ),
@@ -1811,7 +1811,7 @@ function tripal_chado_bundle_instances_info_linker(&$info, $entity_type, $bundle
       ),
       'display' => array(
         'default' => array(
-          'label' => 'inline',
+          'label' => 'hidden',
           'type' => 'schema__alternate_name_formatter',
           'settings' => array(),
         ),