You are here

function bulk_photo_nodes_prepare_fields in Bulk File Nodes 7

Saves all form field values as node properties.

1 call to bulk_photo_nodes_prepare_fields()
bulk_photo_nodes_save_node in ./bulk_photo_nodes.module
Batch operation: Saves an individual bulk photo node.

File

./bulk_photo_nodes.module, line 661
hooks and helper functions for bulk photo node.

Code

function bulk_photo_nodes_prepare_fields(&$node_fields, $node, &$node_overrides, $image_field_name) {
  foreach ($node_fields['right']['node'] as $field_name => $field_values) {

    // Iterate through fields. Convert form values to properties of the node.
    if ($field_name == $image_field_name) {
      continue;
    }
    if ($field_name != 'title') {
      $field_info = field_info_field($field_name);

      // Check if field is empty.
      $empty_function = $field_info['module'] . '_field_is_empty';
      $is_empty = TRUE;
      if (function_exists($empty_function)) {
        $items = field_get_items('node', $node, $field_name);
        $lang = entity_language('node', $node);
        if (is_array($items)) {

          // Multi-value fields.
          foreach ($items as $key => $item) {
            if ($empty_function($item, $field_info)) {

              // Need to make sure to unset any empty values before passing
              // these to the final node, or they will break things.
              unset($field_values[$lang][$key]);
            }
            else {
              $is_empty = FALSE;
            }
          }
        }
        elseif (!is_array($items) && !empty($items)) {
          if (!$empty_function($items, $field_info)) {
            $is_empty = FALSE;
          }
        }
      }
    }
    else {
      $is_empty = empty($field_values);
    }

    // This field is empty for this node, so populate it with batch fields if
    // available; otherwise unset it.
    if ($is_empty) {
      if (is_array($node_overrides[$field_name])) {

        // It is necessary to remove fields which have effectively empty arrays
        // (null leaves). The empty() language construct cannot accept function
        // return values.
        $check_empty = _bulk_photo_nodes_array_filter($node_overrides[$field_name]);
        if (!empty($check_empty)) {
          $node->{$field_name} = $node_overrides[$field_name];
        }
        else {
          unset($node->{$field_name});
        }
      }
      else {
        if ($node_overrides[$field_name]) {
          $node->{$field_name} = $node_overrides[$field_name];
        }
        else {
          unset($node->{$field_name});
        }
      }
    }
    else {
      $node->{$field_name} = $field_values;
    }
  }
  return $node;
}