3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Component\Datetime\DateTimePlus;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\ProcessPluginBase;
9 use Drupal\migrate\Row;
12 * Converts date/datetime from one format to another.
14 * Available configuration keys
15 * - from_format: The source format string as accepted by
16 * @link http://php.net/manual/datetime.createfromformat.php \DateTime::createFromFormat. @endlink
17 * - to_format: The destination format.
18 * - timezone: (deprecated) String identifying the required time zone, see
19 * DateTimePlus::__construct(). The timezone configuration key is deprecated
20 * in Drupal 8.4.x and will be removed before Drupal 9.0.0, use from_timezone
21 * and to_timezone instead.
22 * - from_timezone: String identifying the required source time zone, see
23 * DateTimePlus::__construct().
24 * - to_timezone: String identifying the required destination time zone, see
25 * DateTimePlus::__construct().
26 * - settings: keyed array of settings, see DateTimePlus::__construct().
28 * Configuration keys from_timezone and to_timezone are both optional. Possible
30 * - Both from_timezone and to_timezone are empty. Date will not be converted
31 * and be treated as date in default timezone.
32 * - Only from_timezone is set. Date will be converted from timezone specified
33 * in from_timezone key to the default timezone.
34 * - Only to_timezone is set. Date will be converted from the default timezone
35 * to the timezone specified in to_timezone key.
36 * - Both from_timezone and to_timezone are set. Date will be converted from
37 * timezone specified in from_timezone key to the timezone specified in
42 * Example usage for date only fields
43 * (DateTimeItemInterface::DATE_STORAGE_FORMAT):
48 * from_format: 'm/d/Y'
53 * If the source value was '01/05/1955' the transformed value would be
56 * Example usage for datetime fields
57 * (DateTimeItemInterface::DATETIME_STORAGE_FORMAT):
62 * from_format: 'm/d/Y H:i:s'
63 * to_format: 'Y-m-d\TH:i:s'
67 * If the source value was '01/05/1955 10:43:22' the transformed value would be
68 * 1955-01-05T10:43:22.
70 * Example usage for datetime fields with a timezone and settings:
75 * from_format: 'Y-m-d\TH:i:sO'
76 * to_format: 'Y-m-d\TH:i:s'
77 * from_timezone: 'America/Managua'
80 * validate_format: false
84 * If the source value was '2004-12-19T10:19:42-0600' the transformed value
85 * would be 2004-12-19T10:19:42. Set validate_format to false if your source
86 * value is '0000-00-00 00:00:00'.
88 * @see \DateTime::createFromFormat()
89 * @see \Drupal\Component\Datetime\DateTimePlus::__construct()
90 * @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface
91 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
93 * @MigrateProcessPlugin(
97 class FormatDate extends ProcessPluginBase {
102 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
103 if (empty($value) && $value !== '0' && $value !== 0) {
107 // Validate the configuration.
108 if (empty($this->configuration['from_format'])) {
109 throw new MigrateException('Format date plugin is missing from_format configuration.');
111 if (empty($this->configuration['to_format'])) {
112 throw new MigrateException('Format date plugin is missing to_format configuration.');
115 $fromFormat = $this->configuration['from_format'];
116 $toFormat = $this->configuration['to_format'];
117 if (isset($this->configuration['timezone'])) {
118 @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);
119 $from_timezone = $this->configuration['timezone'];
120 $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : NULL;
123 $system_timezone = date_default_timezone_get();
124 $default_timezone = !empty($system_timezone) ? $system_timezone : 'UTC';
125 $from_timezone = isset($this->configuration['from_timezone']) ? $this->configuration['from_timezone'] : $default_timezone;
126 $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : $default_timezone;
128 $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
130 // Attempts to transform the supplied date using the defined input format.
131 // DateTimePlus::createFromFormat can throw exceptions, so we need to
132 // explicitly check for problems.
134 $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]);
136 catch (\InvalidArgumentException $e) {
137 throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
139 catch (\UnexpectedValueException $e) {
140 throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);