Browse Source

Removed code in the tripal_get_variable that should be in the install and update hooks. Also moved tripal_field_is_empty to the API and added function comments

Stephen Ficklin 6 years ago
parent
commit
48da81ae97

+ 0 - 26
tripal/api/tripal.entities.api.inc

@@ -1005,32 +1005,6 @@ function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE
 function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
   $variable = tripal_get_variable($variable_name);
 
-  if (!$variable) {
-    if($variable_name === 'hide_empty_field'){
-      tripal_insert_variable(
-        'hide_empty_field',
-        'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
-      );
-      $variable = tripal_get_variable($variable_name);
-      if (!$variable) {
-        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;
-    }
-  }
-
   // And then we need to write the new format to the tripal_bundle_variables 
   // table.
   $record = array(

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

@@ -107,6 +107,55 @@ function hook_bundle_instances_info($entity_type, $bundle) {
 
 }
 
+/**
+ * Indicate if a field has an empty value.
+ *
+ * By default, all field values are attached to an entity in the form
+ * $entity->{field_name}[{language}][{delta}].   Tyipcally a field witll then
+ * have a 'value' element:  $entity->{field_name}[{language}][{delta}]['value']
+ * and if that value is empty then the field is considered empty by Tripal.
+ * By default the tripal_field-is_empty() function is used to check all
+ * fields to see if they are empty. However, this fhook can be implemented by
+ * any module to override that behavior.
+ *
+ * @param $field
+ *   A field array.
+ * @param $items
+ *   The array of items attached to entity.
+ * @param $delta
+ *   The specific value to check.  For fields with cardinality greater than
+ *   1 then each value can be checked. Defaults to 0 indicating it will check
+ *   the first value.
+ *
+ * @return
+ *   TRUE if the field value is empty for the given delta, and FALSE if not
+ *   empty.
+ *   
+ * @ingroup tripal_fields_api
+ */
+function tripal_field_is_empty($field, $items, $delta = 0) {
+  
+  // If the $items argument is empty then return TRUE.
+  if (!$items) {
+    return TRUE;
+  }
+  
+  // 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])) {
+    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'])) {
+    return TRUE;
+  }
+  
+  // Otherwise, the field is not empty.
+  return FALSE;
+}
+
 /**
  * Retrieves a list of TripalField types.
  *

+ 1 - 1
tripal/includes/tripal.entity.inc

@@ -567,7 +567,7 @@ function tripal_ajax_attach_field($id) {
     // more investigation.
     $instance = field_info_instance('TripalEntity', $field_name, $entity->bundle);
     $items = field_get_items('TripalEntity', $entity, $field_name);
-    if(count($items) > 0 && tripal_field_is_empty($items[0], $field)) {
+    if(count($items) > 0 && tripal_field_is_empty($field, $items)) {
       return drupal_json_output([
         'id' => $id,
         'content' => '',

+ 0 - 26
tripal/includes/tripal.fields.inc

@@ -913,32 +913,6 @@ function tripal_form_field_ui_display_overview_form_alter(&$form, &$form_state,
   }
 }
 
-/**
- * Implements hook_field_is_empty().
- */
-function tripal_field_is_empty($item, $field) {
-
-  // If the $item argument is empty then return TRUE.
-  if (!$item) {
-    return TRUE;
-  }
-
-  // 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', $item)) {
-    return TRUE;
-  }
-
-  // If there is a value field but there's nothing in it, the the field is
-  // empty.
-  if (array_key_exists('value', $item) and empty($item['value'])) {
-    return TRUE;
-  }
-
-  // Otherwise, the field is not empty.
-  return FALSE;
-}
-
 /**
  * Theme function for all TripalFieldWidget objects.
  *

+ 1 - 1
tripal/tripal.module

@@ -1530,7 +1530,7 @@ function tripal_field_display_TripalEntity_alter(&$display, $context){
     $item = field_get_items('TripalEntity', $context['entity'], $field_name);
     if($item) {
       $field = field_info_field($field_name);
-      if(tripal_field_is_empty($item[0], $field)) {
+      if(tripal_field_is_empty($field, $items)) {
         // Stop the right rail element from rendering.
         drupal_add_css('.' . $field_name.' {display: none;}', 'inline');
       }

+ 2 - 2
tripal_ds/tripal_ds.module

@@ -475,7 +475,7 @@ function tripal_ds_field_display_alter(&$display, $context){
       $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)) {
+        if (tripal_field_is_empty($field, $item)) {
           $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) {
@@ -536,7 +536,7 @@ function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle, $
           // Now check if each child is empty.
           $item = field_get_items('TripalEntity', $context['entity'], $child);
           $field = field_info_field($child);
-          if(tripal_field_is_empty($item[0], $field)){
+          if(tripal_field_is_empty($field, $item)){
             //If any of the fields are not empty do not add the parent, add the specific field.
             // break 2;
             if (!empty($field)) {