浏览代码

hide empty fields now ready for testing on both tripal_ds enabled sites and non tripal_ds sites

Shawna 7 年之前
父节点
当前提交
3e48a65591

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

@@ -769,11 +769,22 @@ function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE
  *   The value of the variable for the given bundle.
  */
 function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
-
   $variable = tripal_get_variable($variable_name);
 
   if (!$variable) {
-    return FALSE;
+    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 {
+      return FALSE;
+    }
   }
 
   // And then we need to write the new format to the tripal_bundle_variables table.

+ 15 - 9
tripal/includes/TripalBundleUIController.inc

@@ -171,17 +171,16 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
     $form['description']['#default_value'] = tripal_get_bundle_variable('description', $bundle->id, '');
   }
 
-  // TODO: (Shawna) Add in form elements to allow the site admin
-  // to set if fields should be shown by defulat if empty.
-  // Tripal default should always be to hide fields if there are no values.
-  $form['hide_empty'] = array(
-    '#type' => 'checkboxes',
+  $empty_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, '');
+  $form['hide_empty_field'] = array(
+    '#type' => 'select',
     '#title' => t('Field Display'),
     '#options' => array(
-      1 => t('Hide empty fields'),
+      'hide' => t('Hide empty fields'),
+      'show' => t('Show empty fields'),
     ),
-    '#description' => t('Check this box if you would like all empty fields hidden.'),
-    '#default_value' => 1,
+    '#description' => t('Choose either to show or hide all empty fields.'),
+    '#default_value' => !empty($empty_fields) ? array($empty_fields,) : array('hide',),
   );
 
   $form['additional_settings'] = array(
@@ -319,7 +318,11 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
  * Validate: Tripal content type edit form.
  */
 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';
+  }
   // VALIDATE: The only tokens used should be those we mentioned under "Available Tokens".
   // PART 1: Set Titles.
   $tokens_available = unserialize($form_state['values']['set_titles']['tokens']);
@@ -384,6 +387,9 @@ function tripal_tripal_bundle_form_submit($form, &$form_state) {
     // Save the description.
     tripal_set_bundle_variable('description', $bundle_entity->id, $form_state['values']['description']);
 
+    // Save the hide_empty_field setting.
+    tripal_set_bundle_variable('hide_empty_field', $bundle_entity->id, $form_state['values']['hide_empty_field']);
+
     // Save the page title format.
     tripal_save_title_format(
       $bundle_entity,

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

@@ -746,26 +746,12 @@ function tripal_field_settings_form_validate($element, &$form_state, $form) {
  * Implements hook_field_formatter_settings_summary().
  */
 function tripal_field_formatter_settings_summary($field, $instance, $view_mode) {
-
-  // TODO: (Shawna)  Add appropriate code here so that all TripalFields have
-  // the ability to change the bundle default about whether they are
-  // shown if they have no items.  We have to put this here rather than in the
-  // TripalFieldFormatter class because not all fields implement this class.
-  $display = $instance['display'][$view_mode];
-  $settings = $display['settings'];
-
-  $summary = '';
-
-  $summary = t('Hide this field if empty.');
   $formatter_class = $field['type'] . '_formatter';
   if (tripal_load_include_field_class($formatter_class)) {
     $formatter = new $formatter_class($field, $instance);
-    $summary = $formatter->settingsSummary($view_mode);
+    $formatter->settingsSummary($view_mode);
   }
-  if(!$summary){
-    $summary = t('Hide this field if empty.');
-  }
-  return $summary;
+  return $formatter;
 }
 
 /**
@@ -773,24 +759,6 @@ function tripal_field_formatter_settings_summary($field, $instance, $view_mode)
  */
 function tripal_field_formatter_settings_form($field, $instance,
     $view_mode, $form, &$form_state) {
-
-  // TODO: (Shawna)  Add form elements here so that all TripalFields have
-  // the ability to change the bundle default about whether they are
-  // shown if they have no items.  We have to put this here rather than in the
-  // TripalFieldFormatter class because not all fields implement this class.
-  $display = $instance['display'][$view_mode];
-  $settings = $display['settings'];
-  $formatter_class = $field['type'] . '_formatter';
-  $elements = array();
-
-  $elements['hide'] = array(
-      '#title' => t('Hide this field if empty?'),
-      '#type' => 'checkbox',
-      '#default_value' => 1,
-      '#required' => TRUE,
-    );
-
-
   if (tripal_load_include_field_class($formatter_class)) {
 
     $formatter = new $formatter_class($field, $instance);

+ 4 - 0
tripal/tripal.install

@@ -39,6 +39,10 @@ function tripal_add_variables() {
       'description',
       'The description of a Tripal Entity type/bundle.'
   );
+  tripal_insert_variable(
+    'hide_empty_field',
+    'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
+  );
 }
 
 /**

+ 47 - 1
tripal/tripal.module

@@ -999,4 +999,50 @@ function tripal_html5_file_value($element, $input = FALSE, &$form_state) {
       return $input;
     }
   }
-}
+}
+
+
+/**
+ *  Implements hook_field_display_alter().
+ * @param $display
+ * @param $context
+ */
+function tripal_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, '');
+
+  if($hide_variable == 'hide'){
+    $field = field_get_items('TripalEntity', $context['entity'], $field_name);
+    if($field) {
+      if(tripal_field_is_empty($field, $field)) {
+            // Stop the right rail element from rendering.
+            drupal_add_css('.'.$field_name.' {display:none;}', 'inline');
+      }
+    }
+  }
+}
+
+/**
+ *  Implements _field_group_table_rows_alter().
+ * @param $element
+ * @param $children
+ */
+function tripal_field_group_table_rows_alter(&$element, &$children) {
+  foreach ($children as $index => $child) {
+    if (is_array($element[$child])) {
+      $bundle = $element[$child]['#bundle'];
+      $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
+      $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, '');
+      if($hide_variable == 'hide'){
+        if ($element[$child][0]['#markup'] == NULL) {
+          // This is an empty row, remove it
+          unset($children[$index]);
+          unset($element[$child]);
+        }
+      }
+    }
+  }
+}
+

+ 1 - 1
tripal_ds/includes/tripal_ds.field_formatter.inc

@@ -82,4 +82,4 @@ function tripal_ds_field_group_pre_render(&$element, $group, &$form) {
       $element['#suffix'] = '</div>';
       break;
   }
-}
+}

+ 33 - 45
tripal_ds/tripal_ds.module

@@ -436,22 +436,20 @@ 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));
-  $entity_info = entity_get_info('TripalEntity');
-  //watchdog('debug', '<pre>$entity_info: '. print_r($entity_info, TRUE) .'</pre>');
+  $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, '');
 
-  if($field_name){
+  if($field_name && ($hide_variable == 'hide')){
     $field = field_get_items('TripalEntity', $context['entity'], $field_name);
     if($field) {
-      if(tripal_ds_field_is_empty($field, $field)) {
-        $parent_field_info = tripal_ds_find_field_group_parent($field_name, 'TripalEntity', $bundle);
+      if(tripal_field_is_empty($field, $field)) {
+        $parent_field_info = tripal_ds_find_field_group_parent($field_name, 'TripalEntity', $bundle, $context);
         if(!empty($parent_field_info)){
           $increment = 0;
           foreach($parent_field_info as $parent_fields => $parent_field){
             // Stop the right rail element from rendering.
-            drupal_add_css('.'.$parent_field_info[$increment]->group_name.' {display:none;}', 'inline');
-            drupal_add_css('.'.$field_name.' {display:none;}', 'inline');
+            drupal_add_css('.'.$parent_field_info[$increment].' {display:none;}', 'inline');
             // Hide any associated menu links.
-            drupal_add_css('#'.$parent_field_info[$increment]->group_name.' {display:none;}', 'inline');
+            drupal_add_css('#'.$parent_field_info[$increment].' {display:none;}', 'inline');
           }
         }
       }
@@ -468,59 +466,49 @@ function tripal_ds_field_display_alter(&$display, $context){
  *
  * @return array
  */
-function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle){
+function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle, $context){
   $field_groups_to_hide = array();
   $increment = 0;
+
   // Get the field groups associated with this bundle.
   $fg_for_bundle = db_select('field_group', 'fg')
     ->fields('fg')
     ->condition('bundle', $bundle, '=')
     ->condition('entity_type', $entity_type, '=')
     ->execute()->fetchAll();
+
   // Run through the field groups looking for the provided $field_name
   foreach ($fg_for_bundle as $field_groups => $field_group) {
    $field_group_data = unserialize($field_group->data);
-    if(!empty($field_group_data['children'][0])){
-      $field_group_child = $field_group_data['children'][0];
-      if($field_group_child == $field_name){
-        //If a field group has the $field_name as a child add it to the list to
-        //return to be hidden.
-        $field_groups_to_hide[$increment] = $field_group;
-        $increment++;
+   // There is a separate function to deal with tables, so disregard.
+    if ($field_group_data['format_type'] == 'table'){
+      // Do nothing
+    }
+    elseif (!empty($field_group_data['children'][0])) {
+      $children = $field_group_data['children'];
+      //If there is more than one child all need to be checked.
+      if (count($children) > 1) {
+        foreach ($children as $kids => $child) {
+          // Now check if each child if empty.
+          $field = field_get_items('TripalEntity', $context['entity'], $child);
+          if(!tripal_field_is_empty($field, $field)){
+            //If any of the fields are not empty do not add the parent.
+            break 2;
+          }
+          else {
+            continue;
+          }
+        }
+      $field_groups_to_hide[$increment] = $field_group->group_name;
+      }
+      elseif($children[0] == $field_name) {
+        $field_groups_to_hide[$increment] = $field_group->group_name;
       }
     }
+    $increment++;
   }
   // Remove duplicate values.
   $field_groups_to_hide = array_unique($field_groups_to_hide);
   return $field_groups_to_hide;
 
 }
-/**
- *  Implements _field_group_table_rows_alter().
- * @param $element
- * @param $children
- * NEEDS TO BE MOVED SO IT IS ONLY CALLED WHEN APPROPRIATE
- */
-function tripal_ds_field_group_table_rows_alter(&$element, &$children) {
-  foreach ($children as $index => $child) {
-    if (is_array($element[$child])) {
-      if ($element[$child]['#formatter'] == 'text_default') {
-        if ($element[$child][0]['#markup'] == NULL) {
-          // This is an empty row, remove it
-          unset($children[$index]);
-          unset($element[$child]);
-        }
-      }
-    }
-  }
-}
-/**
- * Implements hook_field_is_empty().
- */
-function tripal_ds_field_is_empty($item, $field) {
-  if (empty($item[0]['value']) && $item[0]['value'] !== '0') {
-    return TRUE;
-  }
-  return FALSE;
-}
-