Browse Source

Fixed issues with bad ajax code in tripal_ws module. Fixed loading of non auto_attach fields before page load

Stephen Ficklin 6 years ago
parent
commit
8daaf7736b
28 changed files with 144 additions and 100 deletions
  1. 1 3
      docs/user_guide.rst
  2. BIN
      docs/user_guide/configuring_page_display.5.png
  3. 14 0
      docs/user_guide/content_types.rst
  4. 0 0
      docs/user_guide/content_types/configuring_page_display.1.png
  5. 0 0
      docs/user_guide/content_types/configuring_page_display.2.png
  6. 0 0
      docs/user_guide/content_types/configuring_page_display.3.rearrange.png
  7. 0 0
      docs/user_guide/content_types/configuring_page_display.4.png
  8. 4 14
      docs/user_guide/content_types/configuring_page_display.rst
  9. 0 0
      docs/user_guide/content_types/creating_content.create1.png
  10. 0 0
      docs/user_guide/content_types/creating_content.create2.png
  11. 0 0
      docs/user_guide/content_types/creating_content.create3.png
  12. 0 0
      docs/user_guide/content_types/creating_content.create4.png
  13. 0 0
      docs/user_guide/content_types/creating_content.create5.png
  14. 0 0
      docs/user_guide/content_types/creating_content.create6.png
  15. 0 0
      docs/user_guide/content_types/creating_content.create7.png
  16. 0 0
      docs/user_guide/content_types/creating_content.rst
  17. BIN
      docs/user_guide/content_types/field_loading.empty_ajax.png
  18. 26 0
      docs/user_guide/content_types/field_loading.rst
  19. 0 0
      docs/user_guide/content_types/setting_page_urls.1.png
  20. 0 0
      docs/user_guide/content_types/setting_page_urls.2.png
  21. 0 0
      docs/user_guide/content_types/setting_page_urls.rst
  22. 6 2
      tripal/api/tripal.fields.api.inc
  23. 5 5
      tripal/includes/TripalEntityController.inc
  24. 83 47
      tripal/includes/tripal.entity.inc
  25. 3 4
      tripal/tripal.module
  26. 0 6
      tripal_chado/includes/TripalFields/sio__annotation/sio__annotation.inc
  27. 0 1
      tripal_ds/tripal_ds.module
  28. 2 18
      tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

+ 1 - 3
docs/user_guide.rst

@@ -12,9 +12,7 @@ User's Guide
    user_guide/drupal_overview
    user_guide/example_genomics
    user_guide/learn_chado
-   user_guide/creating_content
-   user_guide/setting_page_urls
-   user_guide/configuring_page_display
+   user_guide/content_types
    user_guide/mviews
    user_guide/searching
    user_guide/job_management

BIN
docs/user_guide/configuring_page_display.5.png


+ 14 - 0
docs/user_guide/content_types.rst

@@ -0,0 +1,14 @@
+Working with Content Types
+==========================
+
+New in Tripal v3 is the ability to create your own content types and manage their display!  In previous versions of Tripal a bit of PHP programming was necessary. This is no longer the case.  Tripal v3 comes pre-populated with a variety of content types and provides a web interface that allows you to create your own.  This section provides details for working with content types in Tripal.
+
+.. toctree::
+   :maxdepth: 1
+   :caption: Table of Contents
+   :glob:
+
+   ./content_types/creating_content
+   ./content_types/setting_page_urls
+   ./content_types/configuring_page_display
+   ./content_types/field_loading

+ 0 - 0
docs/user_guide/configuring_page_display.1.png → docs/user_guide/content_types/configuring_page_display.1.png


+ 0 - 0
docs/user_guide/configuring_page_display.2.png → docs/user_guide/content_types/configuring_page_display.2.png


+ 0 - 0
docs/user_guide/configuring_page_display.3.rearrange.png → docs/user_guide/content_types/configuring_page_display.3.rearrange.png


+ 0 - 0
docs/user_guide/configuring_page_display.4.png → docs/user_guide/content_types/configuring_page_display.4.png


+ 4 - 14
docs/user_guide/configuring_page_display.rst → docs/user_guide/content_types/configuring_page_display.rst

@@ -1,16 +1,12 @@
-
-Configuring Page Display
-=========================
-
-
-This is one of the many new exciting features of Tripal v3.x. In this version of Tripal we have taken integration with Drupal Fields to a whole new level representing each piece of content (in Chado or otherwise) as a Drupal Field. What this means for site builders is unprecendented control over content display and arrangement through the administrative user interface --No more editing PHP template files to change the order, grouping or wording of content!
+Configuring Page Layout
+=======================
+This is one of the many new exciting features of Tripal v3. Integration with Drupal Fields has gone to a whole new level. Site builders have unprecendented control over the display of each piece of data through the administrative user interface. Previously, site builders were required to edit PHP template files to change the order, grouping or wording of content.
 
 You can configure the display of a given Tripal Content Type by navigating to **Structure → Tripal Content Types** and then selecting the **Manage Display** link beside the content type you would like to configure.
 
 .. image:: ./configuring_page_display.1.png
 
 
-
 The Manage Display User Interface lists each Drupal Field in the order they will be displayed on the page. Fields are grouped into Tripal Panes by the Tripal DS module and the page is automatically divided into a right and left column. By default the left column contains the table of contents which lists the Tripal Panes available to the user in the order they are listed in this UI. The following screenshots are using the Analysis Content Type for demonstatration.
 
 .. image:: configuring_page_display.2.png
@@ -39,7 +35,7 @@ There may also be data you want to collect from your user but don't want to disp
 Changing Tripal Pane Names
 --------------------------
 
-The name of a Tripal Pane is displayed both in the header of the Pane itself and in the Table of Contents. To change this name, click the gear button to the far right of the Tripal Pane you would like to change. This will bring up a blue pane of settings. Changing the Field Group Label will change the display name of the pane. For example, the following screenshot shows how you would change the "Cross References" Tripal Pane to be labeled "External Resources" instead if that it what you prefer. Then just click the Update button to see your changes take effect.
+If you enabled the `tripal_ds` module during installation then you will have what is called **Panes** into which fields can be grouped. With the `tripal_ds` module all field come pre-organizd into panes.  The name of a pane is displayed both in the header of the pane itself and in the Table of Contents. To change this name, click the gear button to the far right of the Tripal Pane you would like to change. This will bring up a blue pane of settings. Changing the Field Group Label will change the display name of the pane. For example, the following screenshot shows how you would change the "Cross References" Tripal Pane to be labeled "External Resources" instead if that it what you prefer. Then just click the Update button to see your changes take effect.
 
 .. image:: ./configuring_page_display.4.png
 
@@ -49,9 +45,3 @@ Display/Hide Tripal Panes on Page Load
 
 You can also easily control which Tripal Panes you would like displayed to the user on initial page load. By default the Summary Pane is the only one configured to show by default. However, if you would prefer for all panes or even a specific subset of panes to show by default, you can simply click the gear button to the far right of each Tripal Pane you want displayed by default and uncheck the "Hide panel on page load" checkbox. This gives you complete control over which panes you want your user to see first. If more then one pane is displayed by default then they will be shown in the order they are listed on the Manage Display UI.
 
-Display/Hide Empty Fields
--------------------------
-
-By default Tripal v3 hides all empty fields from the user. However like most behaviour in Tripal, this can be configured. If you would prefer to show all fields to the user regardless of whether there is content for that particular page, then navigate to ``Structure → Tripal Content Types`` and then click on the edit link beside the Tripal Content Type you would like to show empty fields for. Near the bottom of this form is a **Field Display** drop-down. Just change this drop-down to "show empty fields" and then click **Save Content Type**. As an example, we have changed this setting for the organism content type and, as you can see below, now you can see all fields (including empty fields like cross references and relationships) available to the organism content type.
-
-.. image:: ./configuring_page_display.5.png

+ 0 - 0
docs/user_guide/creating_content.create1.png → docs/user_guide/content_types/creating_content.create1.png


+ 0 - 0
docs/user_guide/creating_content.create2.png → docs/user_guide/content_types/creating_content.create2.png


+ 0 - 0
docs/user_guide/creating_content.create3.png → docs/user_guide/content_types/creating_content.create3.png


+ 0 - 0
docs/user_guide/creating_content.create4.png → docs/user_guide/content_types/creating_content.create4.png


+ 0 - 0
docs/user_guide/creating_content.create5.png → docs/user_guide/content_types/creating_content.create5.png


+ 0 - 0
docs/user_guide/creating_content.create6.png → docs/user_guide/content_types/creating_content.create6.png


+ 0 - 0
docs/user_guide/creating_content.create7.png → docs/user_guide/content_types/creating_content.create7.png


+ 0 - 0
docs/user_guide/creating_content.rst → docs/user_guide/content_types/creating_content.rst


BIN
docs/user_guide/content_types/field_loading.empty_ajax.png


+ 26 - 0
docs/user_guide/content_types/field_loading.rst

@@ -0,0 +1,26 @@
+Hide Empty Fields and AJAX loading
+==================================
+
+Tripal provides two additional controls for display of fields on a page:
+
+* Hiding fields with no data.
+* Loading fields using AJAX.
+
+You will find two check boxes when editing any content page that gives you these controls.  Navigate to ``Structure → Tripal Content Types`` and then click on any Tripal Content Type.  You will see options similar to the following:
+
+.. image:: ./field_loading.empty_ajax.png
+
+
+Hiding Empty Fields
+-------------------
+The previous sections of this guide instructed how to rearrange fields on a page, hide their titles, and organize them into panes.  However, while there are many fields many of them may not have any data.  All of these fields are present because the data store (e.g. Chado) has the capacity to house the type of data the fields represent, but if you did not load data appropriate for those fields then they will have no data.  
+
+By default Tripal v3 hides all empty fields from the user. However if you would prefer to show all fields to the user regardless of whether there is content for that particular page edit the content type and click the box labeled `Hide Empty Fields` and click the `Save` button at the bottom.  The next time anyone loads any page for the given content type all fields will be shown regardless if they have data.
+
+Using AJAX to Load Fields
+-------------------------
+Depending on the number of fields for your content type and the amount of data that those fields contain you may notice that page loads can take a few seconds to load. AJAX is a method to help decrease load times by allowing the page to load quickly with minimal data and allowing fields with larger amounts of data to load after the initial page load.  Tripal has this setting enabled by default. but you can disable this feature.  Similar to the check box for hiding fields, there is a check box on the content type edit page labeled `Load field using AJAX`. Remove the check for box to disable all AJAX loading of fields and save the content type settings.
+
+.. note::
+ 
+  You can control AJAX loading and hiding of empty fields differently for each conent type.

+ 0 - 0
docs/user_guide/setting_page_urls.1.png → docs/user_guide/content_types/setting_page_urls.1.png


+ 0 - 0
docs/user_guide/setting_page_urls.2.png → docs/user_guide/content_types/setting_page_urls.2.png


+ 0 - 0
docs/user_guide/setting_page_urls.rst → docs/user_guide/content_types/setting_page_urls.rst


+ 6 - 2
tripal/api/tripal.fields.api.inc

@@ -142,13 +142,17 @@ function tripal_field_is_empty($field, $items, $delta = 0) {
   
   // If the field is a tripal field storage API field and there
   // is no value field then the field is empty.
-  if (array_key_exists('tripal_storage_api', $field['storage']['settings']) and !array_key_exists('value', $items[$delta])) {
+  if (array_key_exists('storage', $field) and 
+      array_key_exists('tripal_storage_api', $field['storage']['settings']) and 
+      !array_key_exists('value', $items[$delta])) {
     return TRUE;
   }
   
   // If there is a value field but there's nothing in it, the the field is
   // empty.
-  if (array_key_exists('value', $items[$delta]) and empty($items[$delta]['value'])) {
+  if (array_key_exists($delta, $items) and 
+      array_key_exists('value', $items[$delta]) and 
+      empty($items[$delta]['value'])) {
     return TRUE;
   }
   

+ 5 - 5
tripal/includes/TripalEntityController.inc

@@ -688,13 +688,13 @@ class TripalEntityController extends EntityAPIController {
           // attach then we will ignore it. It can only be set by providing
           // the id in the $field_id array handled previously.
           else {
-            // We only load via AJAX if instructed to do so and only for fields
-            // that are not auto attached.
-            $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle->id);
+            
+            // Do not load fields that are not auto attached.  Instead set
+            // their value to an empty string and set the #processed key to
+            // FALSE.
             if (array_key_exists('settings', $instance) and
                 array_key_exists('auto_attach', $instance['settings']) and
-                $instance['settings']['auto_attach'] == FALSE and
-                $ajax_field == TRUE) {
+                $instance['settings']['auto_attach'] == FALSE) {
 
                // Add an empty value. This will allow the tripal_entity_view()
                // hook to add the necessary prefixes to the field for ajax

+ 83 - 47
tripal/includes/tripal.entity.inc

@@ -496,68 +496,104 @@ function tripal_entity_access($op, $entity = NULL, $account = NULL, $entity_type
   return FALSE;
 }
 
+/**
+ * Implements hook_entity_prepare_view
+ * 
+ * This function is called before building the content array for an entity. It
+ * allows us to load any unattached fields if AJAX is turned off.
+ */
+function tripal_entity_prepare_view($entities, $type, $langcode) {
+  
+  // This is for only TripalEntity content types.
+  if ($type != 'TripalEntity') {
+    return;
+  }
+
+  // Iterate through the entities and instancesa and if AJAX loading is turned
+  // off then we need to load those fields that were not auto attached.
+  foreach ($entities as $entity_id => &$entity) {
+    $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
+    $use_ajax = tripal_get_bundle_variable('ajax_field', $bundle->id);
+    $instances = field_info_instances('TripalEntity', $entity->bundle);
+    foreach ($instances as $instance) {
+      $field_name = $instance['field_name'];
+      $field = field_info_field($field_name);
+      
+      $auto_attach = array_key_exists('auto_attach', $instance['settings']) ? $instance['settings']['auto_attach'] : TRUE;
+      $processed = $entity->{$field_name}['#processed'];
+      
+      // If the field is not ajax loadable and the field is not auto attached
+      // then we need to add the value.
+      if (!$use_ajax and $auto_attach == FALSE and $processed == FALSE) {
+        $temp = tripal_load_entity('TripalEntity', [$entity_id], TRUE, [$field['id']]);
+        reset($temp);
+        $entity->{$field_name} = $temp[$entity_id]->{$field_name};
+        $items = field_get_items('TripalEntity', $entity, $field_name);        
+      }
+    }
+  }
+}
 
 /**
  * Implements hook_entity_view.
  *
  * Here we want to overwite unattached fields with a div box that will be
  * recognized by JavaScript that will then use AJAX to load the field.
- *
  * The tripal_ajax_attach_field() function is called by an AJAX call to
- * retrieve the field.
+ * retrieve the field.  We also remove empty fields that were auto attached.
  */
 function tripal_entity_view($entity, $type, $view_mode, $langcode) {
 
-  if ($type == 'TripalEntity') {
+  // This is for only TripalEntity content types.
+  if ($type != 'TripalEntity') {
+    return;
+  }
     
-    $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
-    $hide_empty = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
-    $use_ajax = tripal_get_bundle_variable('ajax_field', $bundle->id);
-
-    // Iterate through the fields attached to this entity and add IDs to them
-    // as well as some classe for ajax loading.
-    foreach (element_children($entity->content) as $child_name) {
-
-      // 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.
-      $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
-      if (!$instance) {
-        continue;
-      }
-      
-      // Initailize the prefix and suffix for this field.
-      if (!array_key_exists('#prefix', $entity->content[$child_name])) {
-        $entity->content[$child_name]['#prefix'] = '';
-      }
-      if (!array_key_exists('#suffix', $entity->content[$child_name])) {
-        $entity->content[$child_name]['#suffix'] = '';
-      }
-      $class = '';
-      
-      // Check if this is an AJAX loadable field, and if so set the class.
-      $auto_attach = array_key_exists('auto_attach', $instance['settings']) ? $instance['settings']['auto_attach'] : TRUE;
-      $load_via_ajax = FALSE;
-      if ($use_ajax and $auto_attach == FALSE and $entity->{$child_name}['#processed'] == FALSE) {
-        $class = 'class="tripal-entity-unattached"';
-        $load_via_ajax = TRUE;
-      }
-      
-      // Set the prefix and suffix.
-      $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
-      $entity->content[$child_name]['#suffix'] .= '</div>';
-      
-      // Remove any auto attached fields if they are empty.
-      if ($hide_empty and !$load_via_ajax) {
-        $field = field_info_field($instance['field_name']);
-        $items = field_get_items('TripalEntity', $entity, $child_name);
-        if (tripal_field_is_empty($field, $items)) {
-          unset($entity->content[$child_name]);
-        }
+  $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
+  $hide_empty = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+  $use_ajax = tripal_get_bundle_variable('ajax_field', $bundle->id);
+
+  // Iterate through the fields attached to this entity and add IDs to them
+  // as well as some classe for ajax loading.
+  foreach (element_children($entity->content) as $child_name) {
+
+    // 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.
+    $instance = field_info_instance('TripalEntity', $child_name, $entity->bundle);
+    if (!$instance) {
+      continue;
+    }
+    $field = field_info_field($instance['field_name']);
+     
+    // Check if this is an AJAX loadable field, and if so set the class.
+    $class = '';
+    $auto_attach = array_key_exists('auto_attach', $instance['settings']) ? $instance['settings']['auto_attach'] : TRUE;
+    $processed = $entity->{$child_name}['#processed'];
+    if ($use_ajax and $auto_attach == FALSE and $processed == FALSE) {
+      $class = 'class="tripal-entity-unattached"';
+    }
+    
+    // Set the prefix and suffix.
+    if (!array_key_exists('#prefix', $entity->content[$child_name])) {
+      $entity->content[$child_name]['#prefix'] = '';
+    }
+    if (!array_key_exists('#suffix', $entity->content[$child_name])) {
+      $entity->content[$child_name]['#suffix'] = '';
+    }
+    $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" ' . $class . '>';
+    $entity->content[$child_name]['#suffix'] .= '</div>';
+    
+    // Remove any auto attached fields if they are empty.
+    if ($hide_empty and $processed) {
+      $items = field_get_items('TripalEntity', $entity, $child_name);
+      if (tripal_field_is_empty($field, $items)) {
+        unset($entity->content[$child_name]);
       }
     }
   }
   
+  //dpm($entity->content);
   // Add some settings for AJAX to deal with fields.
   $settings = [
     'tripal_display' => [

+ 3 - 4
tripal/tripal.module

@@ -1518,7 +1518,7 @@ function tripal_html5_file_value($element, $input = FALSE, &$form_state) {
  * @param $context
  */
 function tripal_field_display_TripalEntity_alter(&$display, $context){
-  $field = $context['field'];
+/*   $field = $context['field'];
   $field_name = $context['field']['field_name'];
   $bundle = $context['entity']->bundle;
   $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
@@ -1532,7 +1532,7 @@ function tripal_field_display_TripalEntity_alter(&$display, $context){
       // Stop the right rail element from rendering.
       drupal_add_css('.' . $field_name.' {display: none;}', 'inline');
     }
-  }
+  }  */
 }
 
 /**
@@ -1556,8 +1556,7 @@ function tripal_field_group_table_rows_alter(&$element, &$children) {
       // If the hide empty variable is turned on and ajax load is turned off
       // then remove fields from the field group.
       $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
-      $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
-      if($hide_variable == TRUE && $ajax_field == FALSE){
+      if($hide_variable == TRUE){
         $items = $element[$child]['#items'];
         // Case #1: there are no items.
         if (count($items) == 0) {

+ 0 - 6
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation.inc

@@ -328,12 +328,6 @@ class sio__annotation extends ChadoField {
         $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__pub_id'] = $linker->pub_id;
       }
     }
-
-    // If there are no cvterms selected above then remove the stub.
-    // This is needed to ensure this field isn't displayed when there are no annotations.
-    if (sizeof($fcvterms) == 0) {
-      unset($entity->{$field_name});
-    }
   }
 }
 

+ 0 - 1
tripal_ds/tripal_ds.module

@@ -468,7 +468,6 @@ function tripal_ds_field_display_alter(&$display, $context){
     $bundle = $context['entity']->bundle;
     $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
     $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
-    $ajax_variable = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
     $processed = isset($context['entity']->{$field_name}) ? $context['entity']->{$field_name}['#processed'] : false;
 
     if ($field_name && $hide_variable == TRUE && $processed) {

+ 2 - 18
tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

@@ -249,12 +249,7 @@ class TripalContentService_v0_1 extends TripalWebService {
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
     $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
-    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
-    // If ajax is turned off the hide fields functionality will be handled by 
-    // javascript so change the $hide_fields variable to false to avoid any actions.
-    if ($ajax_field == TRUE) {
-      $hide_fields = FALSE;
-    }
+
     // Get information about the fields attached to this bundle and sort them
     // in the order they were set for the display.
     $instances = field_info_instances('TripalEntity', $bundle->name);
@@ -346,12 +341,6 @@ class TripalContentService_v0_1 extends TripalWebService {
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
     $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
-    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
-    // If ajax is turned off the hide fields functionality will be handled by 
-    // javascript so change the $hide_fields variable to false to avoid any actions.
-    if ($ajax_field == TRUE) {
-      $hide_fields = FALSE;
-    }
 
     // Get the field  settings.
     $field_name = $field['field_name'];
@@ -448,12 +437,7 @@ class TripalContentService_v0_1 extends TripalWebService {
     // If the entity is set to hide fields that have no values then we
     // want to honor that in the web services too.
     $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
-    $ajax_field = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
-    // If ajax is turned off the hide fields functionality will be handled by 
-    // javascript so change the $hide_fields variable to false to avoid any actions.
-    if ($ajax_field == TRUE) {
-      $hide_fields = FALSE;
-    }
+
     $new_value = '';
     // If the value is an array rather than a scalar then map the sub elements
     // to controlled vocabulary terms.