Browse Source

created the ajax and hide field variables and makes them available to the javascript file

Shawna Spoor 6 years ago
parent
commit
855cdcce18

+ 13 - 1
tripal/api/tripal.entities.api.inc

@@ -967,8 +967,9 @@ function tripal_update_bundle_field($field_name, $field_info, $entity_type_name,
  * @ingroup tripal_entities_api
  */
 function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
-
+  watchdog('debug', '<pre>$tripal_get_bundle_variable: ' . print_r($bundle_id, true) . '</pre>');
   $variable = tripal_get_variable($variable_name);
+  watchdog('debug', '<pre>$tripal_get_bundle_variable variable: ' . print_r($variable, true) . '</pre>');
 
   // Warn if we can't find the tripal_variable.
   if (!$variable) {
@@ -982,6 +983,7 @@ function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE
     ->condition('var.variable_id', $variable->variable_id)
     ->execute()
     ->fetchField();
+  watchdog('debug', '<pre>$tripal_get_bundle_variable value: ' . print_r($value, true) . '</pre>');
 
   // Warn if the value appears to be empty.
   if (!$value) {
@@ -1017,6 +1019,16 @@ function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
         return FALSE;
       }
     }
+    else if ($variable_name === 'ajax_field') {
+      tripal_insert_variable(
+        'ajax_field',
+        'Structure->Tripal Content Type->edit checkbox to allow for ajax fields for that bundle.'
+      );
+      $variable = tripal_get_variable($variable_name);
+      if (!$variable) {
+        return false;
+      }
+    }
     else {
       return FALSE;
     }

+ 23 - 11
tripal/includes/TripalBundleUIController.inc

@@ -219,19 +219,27 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
   else {
     $form['description']['#default_value'] = tripal_get_bundle_variable('description', $bundle->id, '');
   }
-
-  $empty_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, '');
+  $hide_empty_field_var = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
+  if ($hide_empty_field_var != 0) {
+    $hide_empty_field_var = TRUE;
+  }
+  $ajax_field_var = tripal_get_bundle_variable('ajax_field', $bundle->id);
+  if ($ajax_field_var != 0) {
+    $ajax_field_var = TRUE;
+  }
   $form['hide_empty_field'] = array(
-    '#type' => 'select',
-    '#title' => t('Field Display'),
-    '#options' => array(
-      'hide' => t('Hide empty fields'),
-      'show' => t('Show empty fields'),
-    ),
-    '#description' => t('Choose either to show or hide all empty fields.  If "Show empty fields" is selected then fields will be loaded via AJAX to help speed page loads.'),
-    '#default_value' => !empty($empty_fields) ? array($empty_fields,) : array('hide',),
+    '#type' => 'checkbox',
+    '#title' => t('Hide empty fields'),
+    '#description' => t('Uncheck this box if you would like to show all empty fields.'),
+    '#default_value' => $hide_empty_field_var,
   );
 
+  $form['ajax_field'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Load field using AJAX'),
+    '#description' => t('Uncheck this box if you do not want field data to load by ajax, this may signifiantly increase page load times.'),
+    '#default_value' => $ajax_field_var,
+  );
   $form['additional_settings'] = array(
     '#type' => 'vertical_tabs',
     '#weight' => 99,
@@ -393,7 +401,10 @@ function tripal_tripal_bundle_form_validate($form, $form_state) {
   // VALIDATE: That there is a value passed for the hide_empty_field option. If
   // no value passed default to hide field.
   if(empty($form_state['values']['hide_empty_field'])){
-    $form_state['values']['hide_empty_field'] = 'hide';
+    $form_state['values']['hide_empty_field'] = TRUE;
+  }
+  if (empty($form_state['values']['ajax_field'])) {
+    $form_state['values']['ajax_field'] = TRUE;
   }
   // VALIDATE: The only tokens used should be those we mentioned under "Available Tokens".
   // PART 1: Set Titles.
@@ -464,6 +475,7 @@ function tripal_tripal_bundle_form_submit($form, &$form_state) {
 
     // Save the hide_empty_field setting.
     tripal_set_bundle_variable('hide_empty_field', $bundle->id, $form_state['values']['hide_empty_field']);
+    tripal_set_bundle_variable('ajax_field', $bundle->id, $form_state['values']['ajax_field']);
 
     // Save the page title format.
     tripal_save_title_format(

+ 2 - 2
tripal/includes/TripalEntityController.inc

@@ -689,11 +689,11 @@ class TripalEntityController extends EntityAPIController {
           else {
             // We only load via AJAX if empty fields are not hidden.
             $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
-            $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
+            $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, TRUE);
             if (array_key_exists('settings', $instance) and
                 array_key_exists('auto_attach', $instance['settings']) and
                 $instance['settings']['auto_attach'] == FALSE and
-                $hide_variable == 'show') {
+                $hide_variable == FALSE) {
 
                // Add an empty value. This will allow the tripal_entity_view()
                // hook to add the necessary prefixes to the field for ajax

+ 22 - 0
tripal/tripal.install

@@ -50,6 +50,10 @@ function tripal_add_variables() {
     'hide_empty_field',
     'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
   );
+  tripal_insert_variable(
+    'ajax_field',
+    'Structure->Tripal Content Type->edit checkbox for ajax fields for that bundle.'
+  );
 }
 
 /**
@@ -1209,3 +1213,21 @@ function tripal_update_7312() {
   }
 }
 
+
+/**
+ * Adds a tripal_storage_api setting to all field storage details
+ */
+function tripal_update_7313() {
+  $transaction = db_transaction();
+  try {
+    tripal_insert_variable(
+      'ajax_field',
+      'Structure->Tripal Content Type->edit checkbox ajax fields for that bundle.'
+    );
+  } catch (\PDOException $e) {
+    $transaction->rollback();
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: ' . $error);
+  }
+}
+

+ 4 - 4
tripal/tripal.module

@@ -1524,8 +1524,8 @@ function tripal_field_display_TripalEntity_alter(&$display, $context){
 
   // Hide fields that are empty, but only if the hide_empty_field variable
   // is set to 'hide' for this bundel.
-  $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
-  if($hide_variable == 'hide'){
+  $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, TRUE);
+  if($hide_variable == TRUE){
     $item = field_get_items('TripalEntity', $context['entity'], $field_name);
     if($item) {
       $field = field_info_field($field_name);
@@ -1557,8 +1557,8 @@ function tripal_field_group_table_rows_alter(&$element, &$children) {
 
       // If the hide empty variable is turned on then remove fields from
       // the field group.
-      $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
-      if($hide_variable == 'hide'){
+      $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, TRUE);
+      if($hide_variable == TRUE){
         $items = $element[$child]['#items'];
         // Case #1: there are no items.
         if (count($items) == 0) {

+ 21 - 2
tripal_ds/tripal_ds.module

@@ -467,9 +467,10 @@ function tripal_ds_field_display_alter(&$display, $context){
     $field_name = $context['field']['field_name'];
     $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, 'hide');
+    $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id);
+    $ajax_variable = tripal_get_bundle_variable('ajax_field', $bundle_info->id);
 
-    if ($field_name && ($hide_variable == 'hide')) {
+    if ($field_name && ($hide_variable == TRUE)) {
       $item = field_get_items('TripalEntity', $context['entity'], $field_name);
       $field = field_info_field($field_name);
       if ($item) {
@@ -486,6 +487,24 @@ function tripal_ds_field_display_alter(&$display, $context){
         }
       }
     }
+    if ($field_name && ($ajax_variable == TRUE)) {
+      $item = field_get_items('TripalEntity', $context['entity'], $field_name);
+      $field = field_info_field($field_name);
+      if ($item) {
+        if (tripal_field_is_empty($item[0], $field)) {
+          $parent_field_info = tripal_ds_find_field_group_parent($field_name, 'TripalEntity', $bundle, $context);
+          if (!empty($parent_field_info)) {
+            foreach ($parent_field_info as $parent_key => $parent_field) {
+               // We want to use JavaScript to remove the fields rather than
+               // CSS to hide them so that when users theme the table of
+               // contents using CSS they aren't theming empty rows.
+              drupal_add_js('jQuery(document).ready(function () { jQuery("#' . $parent_field_info[$parent_key] . '").parents(".views-row").remove() });', 'inline');
+            }
+          }
+        }
+      }
+    }
+    drupal_add_js(array('tripal_ds' => array('tripal_field_settings_hide' => $hide_variable, 'tripal_field_settings_ajax' => $ajax_variable)), 'setting');
   }
 }
 

+ 3 - 3
tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc

@@ -248,7 +248,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, 'hide');
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, TRUE);
 
     // Get information about the fields attached to this bundle and sort them
     // in the order they were set for the display.
@@ -340,7 +340,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, 'hide');
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, TRUE);
 
     // Get the field  settings.
     $field_name = $field['field_name'];
@@ -436,7 +436,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, 'hide');
+    $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, TRUE);
 
     $new_value = '';
     // If the value is an array rather than a scalar then map the sub elements