You are here

function field_collection_item_is_empty in Field collection 7

Determines whether a field collection item entity is empty based on the collection-fields.

3 calls to field_collection_item_is_empty()
field_collection_field_attach_form in ./field_collection.module
Implements hook_field_attach_form().
field_collection_field_is_empty in ./field_collection.module
Implements hook_field_is_empty().
field_collection_field_widget_embed_validate in ./field_collection.module
FAPI validation of an individual field collection element.

File

./field_collection.module, line 791
Module implementing field collection field type.

Code

function field_collection_item_is_empty(FieldCollectionItemEntity $item) {
  $instances = field_info_instances('field_collection_item', $item->field_name);
  $is_empty = TRUE;

  // Check whether all fields are booleans.
  $all_boolean = $instances && !(bool) array_filter($instances, '_field_collection_field_is_not_boolean');
  foreach ($instances as $instance) {
    $field_name = $instance['field_name'];
    $field = field_info_field($field_name);

    // Boolean fields as those are always considered non-empty, thus their
    // information is not useful and can be skipped by default.
    if (!$all_boolean && $field['type'] === 'list_boolean') {
      continue;
    }

    // Determine the list of languages to iterate on.
    $languages = field_available_languages('field_collection_item', $field);
    foreach ($languages as $langcode) {
      if (!empty($item->{$field_name}[$langcode])) {

        // If at least one collection-field is not empty; the
        // field collection item is not empty.
        foreach ($item->{$field_name}[$langcode] as $field_item) {
          if (!module_invoke($field['module'], 'field_is_empty', $field_item, $field)) {
            $is_empty = FALSE;
          }
        }
      }
    }
  }

  // Allow other modules a chance to alter the value before returning.
  drupal_alter('field_collection_is_empty', $is_empty, $item);
  return $is_empty;
}