You are here

public function FeedsDateTimeElement::buildDateField in Feeds 8.2

Build a entity's date field from our object.

Parameters

object $entity: The entity to build the date field on.

str $field_name: The name of the field to build.

int $delta: The delta in the field.

File

lib/Drupal/feeds/FeedsDateTimeElement.php, line 99

Class

FeedsDateTimeElement
Defines a date element of a parsed result (including ranges, repeat).

Namespace

Drupal\feeds

Code

public function buildDateField($entity, $field_name, $delta = 0) {
  $info = field_info_field($field_name);
  $oldfield = FeedsDateTimeElement::readDateField($entity, $field_name, $delta);

  // Merge with any preexisting objects on the field; we take precedence.
  $oldfield = $this
    ->merge($oldfield);
  $use_start = $oldfield->start;
  $use_end = $oldfield->end;

  // Set timezone if not already in the FeedsDateTime object
  $temp = new FeedsDateTime(NULL, new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  if ($use_start) {
    $use_start = $use_start
      ->merge($temp);
    $use_start
      ->setTimezone(new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  }
  if ($use_end) {
    $use_end = $use_end
      ->merge($temp);
    $use_end
      ->setTimezone(new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  }
  $db_tz = new DateTimeZone(DATETIME_STORAGE_TIMEZONE);
  if (!isset($entity->{$field_name})) {
    $entity->{$field_name} = array(
      'und' => array(),
    );
  }
  if ($use_start) {
    $entity->{$field_name}['und'][$delta]['timezone'] = $use_start
      ->getTimezone()
      ->getName();
    $entity->{$field_name}['und'][$delta]['offset'] = $use_start
      ->getOffset();
    $use_start
      ->setTimezone($db_tz);
    $entity->{$field_name}['und'][$delta]['date'] = $use_start;

    /**
     * @todo the date_type_format line could be simplified based upon a patch
     *   DO issue #259308 could affect this, follow up on at some point.
     *   Without this, all granularity info is lost.
     *   $use_start->format(date_type_format($field['type'], $use_start->granularity));
     */
    $entity->{$field_name}['und'][$delta]['value'] = $use_start
      ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  }
  if ($use_end) {

    // Don't ever use end to set timezone (for now)
    $entity->{$field_name}['und'][$delta]['offset2'] = $use_end
      ->getOffset();
    $use_end
      ->setTimezone($db_tz);
    $entity->{$field_name}['und'][$delta]['date2'] = $use_end;
    $entity->{$field_name}['und'][$delta]['value2'] = $use_end
      ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  }
}