The patch for supporting multiple values in the #697842: Support array of values for dates issue and the patch for supporting empty values in Date in the #857216: Behavior on importing empty/NULL/invalid dates issue edit the same lines of codes in the date.inc file.

Here is what I have for supporting multiple values.

function date_feeds_set_target($source, $entity, $target, $feed_element) {
  list($field_name, $sub_field) = explode(':', $target, 2);
  if (!($feed_element instanceof FeedsDateTimeElement)) {
    if (is_array($feed_element)) {
      $delta = 0;
      foreach ($feed_element as $f) {
        if ($sub_field == 'end') {
          $array_element = new FeedsDateTimeElement(NULL, $f);
        }
        else {
          $array_element = new FeedsDateTimeElement($f, NULL);
        }
        $array_element->buildDateField($entity, $field_name, $delta);
        $delta++;
      }

    }
    else {
      if ($sub_field == 'end') {
        $feed_element = new FeedsDateTimeElement(NULL, $feed_element);
      }
      else {
        $feed_element = new FeedsDateTimeElement($feed_element, NULL);
      }
      $feed_element->buildDateField($entity, $field_name);
    }
  }
}

I tried this per the empty values patch, but it doesn't seem to work.

function date_feeds_set_target($source, $entity, $target, $feed_element) {
  list($field_name, $sub_field) = explode(':', $target, 2);
  if (!($feed_element instanceof FeedsDateTimeElement)) {
    if (is_array($feed_element)) {
      $delta = 0;
      foreach ($feed_element as $f) {
        if ($sub_field == 'end') {
          $array_element = new FeedsDateTimeElement(NULL, $f);
        }
        else {
          $array_element = new FeedsDateTimeElement($f, NULL);
        }
        $array_element->buildDateField($entity, $field_name, $delta);
        $delta++;
      }

    }
    else {
    if (!is_numeric($feed_element) && !strtotime($feed_element)) {
        $feed_element = new FeedsDateTimeElement(NULL, NULL);
    }
    elseif ($sub_field == 'end') {
        $feed_element = new FeedsDateTimeElement(NULL, $feed_element);
      }
      else {
        $feed_element = new FeedsDateTimeElement($feed_element, NULL);
      }
      $feed_element->buildDateField($entity, $field_name);
    }
  }
}

Anyone have any thoughts on what I am doing wrong?

Comments

liquid06’s picture

I've been assembling a feeds patchwork quilt and I ran into the same thing. The data I'm trying to import looks kind of like this:

Title Years
Unicorn Tail Feathers 1899;1920
Moonbeams 2000;2011

I set up feeds_tamper (the latest -dev seems to be the best version) to explode the date field on semicolon. Then on feeds: the patch here #697842: Support array of values for dates seems quite a bit bigger, so I applied that one first (comment #39.) After applying that, the patch in #1537776: Importing a single 21st century year defaults to the current year failed. So I tried to apply those changes by hand.

Here's the full date.inc file that seems to work for my case.

<?php
/**
 * @file
 * On behalf implementation of Feeds mapping API for date
 */

/**
 * Implements hook_feeds_processor_targets_alter().
 *
 * @see FeedsNodeProcessor::getMappingTargets().
 *
 * @todo Only provides "end date" target if field allows it.
 */
function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  foreach (
field_info_instances($entity_type, $bundle_name) as $name => $instance) {
   
$info = field_info_field($name);
    if (
in_array($info['type'], array('date', 'datestamp', 'datetime'))) {
     
$targets[$name . ':start'] = array(
       
'name' => t('@name: Start', array('@name' => $instance['label'])),
       
'callback' => 'date_feeds_set_target',
       
'description' => t('The start date for the @name field. Also use if mapping both start and end.', array('@name' => $instance['label'])),
       
'real_target' => $name,
      );
     
$targets[$name . ':end'] = array(
       
'name' => t('@name: End', array('@name' => $instance['label'])),
       
'callback' => 'date_feeds_set_target',
       
'description' => t('The end date for the @name field.', array('@name' => $instance['label'])),
       
'real_target' => $name,
      );
    }
  }
}

/**
 * Implements hook_feeds_set_target().
 *
 * @param $node
 *   The target node.
 * @param $field_name
 *   The name of field on the target node to map to.
 * @param $feed_element
 *   The value to be mapped. Should be either a (flexible) date string
 *   or a FeedsDateTimeElement object.
 *
 */
function date_feeds_set_target($source, $entity, $target, $feed_element) {
  list(
$field_name, $sub_field) = explode(':', $target, 2);
  if (!(
$feed_element instanceof FeedsDateTimeElement)) {
    if (
is_array($feed_element)) {
     
$delta = 0;
      foreach (
$feed_element as $f) {
        if (empty(
$f) || !is_numeric($f) && !date_create($f)) {
         
$array_element = new FeedsDateTimeElement(NULL, NULL);
        }
        elseif (
$sub_field == 'end') {
         
$array_element = new FeedsDateTimeElement(NULL, $f);
        }
        else {
         
$array_element = new FeedsDateTimeElement($f, NULL);
        }
       
$array_element->buildDateField($entity, $field_name, $delta);
       
$delta++;
      }
    }
    else {
      if (
$sub_field == 'end') {
       
$feed_element = new FeedsDateTimeElement(NULL, $feed_element);
      }
      else {
       
$feed_element = new FeedsDateTimeElement($feed_element, NULL);
      }
     
$feed_element->buildDateField($entity, $field_name);
    }
  }
}
?>
colan’s picture

Status:Active» Closed (duplicate)