You are here

function timefield_feeds_set_target in Timefield 7

Callback for timefield_feeds_processor_targets_alter().

Parameters

object $source: Field mapper source settings.

object $entity: An entity object, for instance a node object.

string $target: A string identifying the target on the node.

string $element_value: The value to populate the target with.

array $mapping: Associative array of the mapping settings from the per mapping configuration form.

1 string reference to 'timefield_feeds_set_target'
timefield_feeds_processor_targets_alter in ./timefield.feeds.inc
Implements hook_feeds_processor_targets_alter().

File

./timefield.feeds.inc, line 48
Integration with the Feeds module.

Code

function timefield_feeds_set_target($source, $entity, $target, $element_value, $mapping) {
  if (empty($element_value)) {
    return;
  }

  // Handle non-multiple value fields.
  if (!is_array($element_value)) {
    $element_value = array(
      $element_value,
    );
  }

  // Get the timefield field name and sub_field (start / end).
  list($field_name, $sub_field) = explode(':', $target);

  // If the field is already set on the given entity, update the existing value
  // array, otherwise, start with a fresh field value array.
  $field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();

  // Loop over the field values array.
  foreach ($element_value as $delta => $value) {

    // Make sure we have a valid time before trying to set the value on the field.
    $date_value = date_parse($value);
    if (empty($date_value['error_count'])) {
      $parsed_value = timefield_time_to_integer($value);
      if ($parsed_value) {
        $sub_field = $sub_field == 'start' ? 'value' : 'value2';
        $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
        $field[$language][$delta][$sub_field] = $parsed_value;
      }
    }
  }

  // Add the field to the entity definition.
  $entity->{$field_name} = $field;
}