You are here

function _field_unset_empty in Entity Construction Kit (ECK) 7

When an entity form is submitted, field for which no information was inputed are still returned, then if we submit that data, empty rows are created in those field databases cluttering them. This function checks and makes sure that the data returned for a field is not empty and unsets it if it is, so no empty data will be added to the database.

Parameters

$field_name: The name of the field.

$data: The data for the field: It usually has this format array(lang => array( 0 => array( <field stuff> ), 1 => ...));

File

./eck.module, line 278
ENTITY CONSTRUCTION KIT

Code

function _field_unset_empty($field_name, $data) {

  // If there is a value we need to check that it is not empty.
  $info = field_info_field($field_name);
  foreach ($data[LANGUAGE_NONE] as $key => $values) {
    $empty = TRUE;
    foreach (array_keys($info['columns']) as $index) {
      if (!empty($values[$index])) {
        $empty = FALSE;
      }
    }
    if ($empty) {
      unset($data[LANGUAGE_NONE][$key]);
    }
  }
  return $data;
}