From 671993e4bc4a5aa5741f1dbd2d79d53e1088fb41 Mon Sep 17 00:00:00 2001
From: Pierre Buyle <pierre@buyle.org>
Date: Mon, 14 Nov 2011 10:21:02 +0100
Subject: [PATCH] Issue #1276258 by mongolito404: Completely hide empty field collections.

---
 field_collection.module |   28 ++++++++++++++++++++++++++++
 1 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/field_collection.module b/field_collection.module
index a43a559..1d24ddc 100644
--- a/field_collection.module
+++ b/field_collection.module
@@ -687,6 +687,7 @@ function field_collection_field_formatter_info() {
         'delete' => t('Delete'),
         'add' => t('Add'),
         'description' => TRUE,
+        'hide_empty' => FALSE,
       ),
     ),
     'field_collection_view' => array(
@@ -698,6 +699,7 @@ function field_collection_field_formatter_info() {
         'add' => t('Add'),
         'description' => TRUE,
         'view_mode' => 'full',
+        'hide_empty' => FALSE,
       ),
     ),
     'field_collection_fields' => array(
@@ -705,6 +707,7 @@ function field_collection_field_formatter_info() {
       'field types' => array('field_collection'),
       'settings' =>  array(
         'view_mode' => 'full',
+        'hide_empty' => FALSE,
       ),
     ),
   );
@@ -763,6 +766,13 @@ function field_collection_field_formatter_settings_form($field, $instance, $view
     );
   }
 
+  $elements['hide_empty'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Hide empty collection'),
+    '#default_value' => $settings['hide_empty'],
+    '#description' => t('If enabled, nothing will be displayed for an empty collection (not even the add link).'),
+  );
+
   return $elements;
 }
 
@@ -801,6 +811,24 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i
   $element = array();
   $settings = $display['settings'];
 
+  // Don't display anything for an empty collection.
+  if (!empty($settings['hide_empty'])) {
+    $empty = TRUE;
+    // Chec if we have some items
+    if (!empty($items)) {
+      // Search for at least one non empty item
+      foreach ($items as $item) {
+        if (!empty($items[0]['value'])) {
+          $empty = FALSE;
+          break;
+        }
+      }
+    }
+    if ($empty) {
+      return NULL;
+    }
+  }
+
   switch ($display['type']) {
     case 'field_collection_list':
 
-- 
1.7.1

