Browse Source

issue 46 hide empty panes by default intial code

Shawna 7 years ago
parent
commit
edf05ba1ac

+ 6 - 1
tripal/includes/TripalBundleUIController.inc

@@ -180,7 +180,12 @@ function tripal_tripal_bundle_form($form, &$form_state, $entityDataType) {
   // 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' => 'checkbox',
+    '#title' => t('Hide Empty Fields'),
+    '#description' => t('Check this box if you would like all empty fields hidden.'),
+    '#default_value' => 1,
+  );
   // Set Title Format.
   //-------------------------
   $title_format = tripal_get_title_format($bundle);

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

@@ -240,6 +240,7 @@ function tripal_field_formatter_view($entity_type, $entity, $field,
     $formatter = new $formatter_class($field, $instance);
     $formatter->view($element, $entity_type, $entity, $langcode, $items, $display);
   }
+
   return $element;
 }
 
@@ -750,12 +751,22 @@ function tripal_field_formatter_settings_summary($field, $instance, $view_mode)
   // 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.');
 
+  watchdog('debug', '<pre>$summary: '. print_r($summary, TRUE) .'</pre>');
   $formatter_class = $field['type'] . '_formatter';
   if (tripal_load_include_field_class($formatter_class)) {
     $formatter = new $formatter_class($field, $instance);
-    return $formatter->settingsSummary($view_mode);
+    $summary = $formatter->settingsSummary($view_mode);
+    watchdog('debug', '<pre>$summary insdie the if: '. print_r($summary, TRUE) .'</pre>');
+
   }
+  return $summary;
 }
 
 /**
@@ -768,9 +779,19 @@ function tripal_field_formatter_settings_form($field, $instance,
   // 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);

+ 1 - 1
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -42,7 +42,7 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
 
   // Convert the fields into a key/value list of fields and their values.
   $field_vals = tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $entity);
-dpm($field_vals);
+
 
   // First, write the record for the base table.  If we have a record id then
   // this is an update and we need to set the primary key.  If not, then this

+ 1 - 1
tripal_ds/includes/tripal_ds.inc

@@ -7,4 +7,4 @@ function tripal_ds_ctools_plugin_api($module = NULL, $api = NULL){
     if ($module == "ds" && $api == "ds") {
         return array("version" => "1");
     }
-}
+}

+ 98 - 0
tripal_ds/tripal_ds.module

@@ -426,3 +426,101 @@ function tripal_ds_update_ds_layout($bundle_name, $field_name, $tripal_pane_name
       ->execute()
       ->fetchField();
 */
+
+/**
+ *  Implements hook_field_display_alter().
+ * @param $display
+ * @param $context
+ */
+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>');
+
+  if($field_name){
+    $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(!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');
+            // Hide any associated menu links.
+            drupal_add_css('#'.$parent_field_info[$increment]->group_name.' {display:none;}', 'inline');
+          }
+        }
+      }
+    }
+  }
+}
+/*
+
+*/
+/**
+ * @param $field_name
+ * @param $entity_type
+ * @param $bundle
+ *
+ * @return array
+ */
+function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle){
+  $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++;
+      }
+    }
+  }
+  // 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;
+}
+