You are here

function scheduler_feeds_set_target in Scheduler 8

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

Mapping callback for the Feeds module.

@todo Port to Drupal 8.

See also

https://www.drupal.org/node/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 691
Scheduler publishes and unpublishes nodes 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 then use it to set the target field in the node.
  if (is_numeric($timestamp) && $timestamp > 0) {
    $entity->{$target} = $timestamp;
  }
}