X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmigrate%2Fsrc%2FPlugin%2Fmigrate%2Fprocess%2FFormatDate.php;h=c9b75a9d7c23a74936da02349046bc732adae2b3;hb=5b8bb166bfa98770daef9de5c127fc2e6ef02340;hp=b197f17f60f6c7dc8a6bb737211726bfed61a618;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php b/web/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php index b197f17f6..c9b75a9d7 100644 --- a/web/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php +++ b/web/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php @@ -15,13 +15,32 @@ use Drupal\migrate\Row; * - from_format: The source format string as accepted by * @link http://php.net/manual/datetime.createfromformat.php \DateTime::createFromFormat. @endlink * - to_format: The destination format. - * - timezone: String identifying the required time zone, see + * - timezone: (deprecated) String identifying the required time zone, see + * DateTimePlus::__construct(). The timezone configuration key is deprecated + * in Drupal 8.4.x and will be removed before Drupal 9.0.0, use from_timezone + * and to_timezone instead. + * - from_timezone: String identifying the required source time zone, see + * DateTimePlus::__construct(). + * - to_timezone: String identifying the required destination time zone, see * DateTimePlus::__construct(). * - settings: keyed array of settings, see DateTimePlus::__construct(). * + * Configuration keys from_timezone and to_timezone are both optional. Possible + * input variants: + * - Both from_timezone and to_timezone are empty. Date will not be converted + * and be treated as date in default timezone. + * - Only from_timezone is set. Date will be converted from timezone specified + * in from_timezone key to the default timezone. + * - Only to_timezone is set. Date will be converted from the default timezone + * to the timezone specified in to_timezone key. + * - Both from_timezone and to_timezone are set. Date will be converted from + * timezone specified in from_timezone key to the timezone specified in + * to_timezone key. + * * Examples: * - * Example usage for date only fields (DATETIME_DATE_STORAGE_FORMAT): + * Example usage for date only fields + * (DateTimeItemInterface::DATE_STORAGE_FORMAT): * @code * process: * field_date: @@ -34,7 +53,8 @@ use Drupal\migrate\Row; * If the source value was '01/05/1955' the transformed value would be * 1955-01-05. * - * Example usage for datetime fields (DATETIME_DATETIME_STORAGE_FORMAT): + * Example usage for datetime fields + * (DateTimeItemInterface::DATETIME_STORAGE_FORMAT): * @code * process: * field_time: @@ -54,17 +74,20 @@ use Drupal\migrate\Row; * plugin: format_date * from_format: 'Y-m-d\TH:i:sO' * to_format: 'Y-m-d\TH:i:s' - * timezone: 'America/Managua' + * from_timezone: 'America/Managua' + * to_timezone: 'UTC' * settings: * validate_format: false * source: event_time * @endcode * * If the source value was '2004-12-19T10:19:42-0600' the transformed value - * would be 2004-12-19T10:19:42. + * would be 2004-12-19T10:19:42. Set validate_format to false if your source + * value is '0000-00-00 00:00:00'. * * @see \DateTime::createFromFormat() * @see \Drupal\Component\Datetime\DateTimePlus::__construct() + * @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface * @see \Drupal\migrate\Plugin\MigrateProcessInterface * * @MigrateProcessPlugin( @@ -77,7 +100,7 @@ class FormatDate extends ProcessPluginBase { * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { - if (empty($value)) { + if (empty($value) && $value !== '0' && $value !== 0) { return ''; } @@ -91,14 +114,24 @@ class FormatDate extends ProcessPluginBase { $fromFormat = $this->configuration['from_format']; $toFormat = $this->configuration['to_format']; - $timezone = isset($this->configuration['timezone']) ? $this->configuration['timezone'] : NULL; + if (isset($this->configuration['timezone'])) { + @trigger_error('Configuration key "timezone" is deprecated in 8.4.x and will be removed before Drupal 9.0.0, use "from_timezone" and "to_timezone" instead. See https://www.drupal.org/node/2885746', E_USER_DEPRECATED); + $from_timezone = $this->configuration['timezone']; + $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : NULL; + } + else { + $system_timezone = date_default_timezone_get(); + $default_timezone = !empty($system_timezone) ? $system_timezone : 'UTC'; + $from_timezone = isset($this->configuration['from_timezone']) ? $this->configuration['from_timezone'] : $default_timezone; + $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : $default_timezone; + } $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : []; // Attempts to transform the supplied date using the defined input format. // DateTimePlus::createFromFormat can throw exceptions, so we need to // explicitly check for problems. try { - $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $timezone, $settings)->format($toFormat); + $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]); } catch (\InvalidArgumentException $e) { throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);