You are here

function scheduler_feeds_set_target in Scheduler 2.x

Same name and namespace in other branches
  1. 8 scheduler.module \scheduler_feeds_set_target()
  2. 7 scheduler.module \scheduler_feeds_set_target()

Mapping callback for the Feeds module.

@todo Port to Drupal 8.

See also

https://www.drupal.org/project/scheduler/issues/2651354

1 string reference to 'scheduler_feeds_set_target'
scheduler_feeds_processor_targets_alter in ./scheduler.module
Implements hook_feeds_processor_targets_alter().

File

./scheduler.module, line 988
Scheduler publishes and unpublishes entities on dates specified by the user.

Code

function scheduler_feeds_set_target($source, $entity, $target, $value, $mapping) {

  // We expect a string or integer, but can accomodate an array, by taking the
  // first item. Use trim() so that a string of blanks is reduced to empty.
  $value = is_array($value) ? trim(reset($value)) : trim($value);

  // Convert input from parser to timestamp form. If $value is empty or blank
  // then strtotime() must not be used, otherwise it returns the current time.
  if (!empty($value) && !is_numeric($value)) {
    if (!($timestamp = strtotime($value))) {

      // Throw an exception if the date format was not recognized.
      $mapping_source = $mapping['source'];
      throw new FeedsValidationException("Value '{$value}' for '{$mapping_source}' could not be converted to a valid '{$target}' date.");
    }
  }
  else {
    $timestamp = $value;
  }

  // If the timestamp is valid, use it to set the target field in the entity.
  if (is_numeric($timestamp)) {
    $entity->{$target} = $timestamp;
  }
}