Pull merge.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / FormatDate.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
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;
10
11 /**
12  * Converts date/datetime from one format to another.
13  *
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().
27  *
28  * Configuration keys from_timezone and to_timezone are both optional. Possible
29  * input variants:
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
38  *   to_timezone key.
39  *
40  * Examples:
41  *
42  * Example usage for date only fields
43  * (DateTimeItemInterface::DATE_STORAGE_FORMAT):
44  * @code
45  * process:
46  *   field_date:
47  *     plugin: format_date
48  *     from_format: 'm/d/Y'
49  *     to_format: 'Y-m-d'
50  *     source: event_date
51  * @endcode
52  *
53  * If the source value was '01/05/1955' the transformed value would be
54  * 1955-01-05.
55  *
56  * Example usage for datetime fields
57  * (DateTimeItemInterface::DATETIME_STORAGE_FORMAT):
58  * @code
59  * process:
60  *   field_time:
61  *     plugin: format_date
62  *     from_format: 'm/d/Y H:i:s'
63  *     to_format: 'Y-m-d\TH:i:s'
64  *     source: event_time
65  * @endcode
66  *
67  * If the source value was '01/05/1955 10:43:22' the transformed value would be
68  * 1955-01-05T10:43:22.
69  *
70  * Example usage for datetime fields with a timezone and settings:
71  * @code
72  * process:
73  *   field_time:
74  *     plugin: format_date
75  *     from_format: 'Y-m-d\TH:i:sO'
76  *     to_format: 'Y-m-d\TH:i:s'
77  *     from_timezone: 'America/Managua'
78  *     to_timezone: 'UTC'
79  *     settings:
80  *       validate_format: false
81  *     source: event_time
82  * @endcode
83  *
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'.
87  *
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
92  *
93  * @MigrateProcessPlugin(
94  *   id = "format_date"
95  * )
96  */
97 class FormatDate extends ProcessPluginBase {
98
99   /**
100    * {@inheritdoc}
101    */
102   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
103     if (empty($value) && $value !== '0' && $value !== 0) {
104       return '';
105     }
106
107     // Validate the configuration.
108     if (empty($this->configuration['from_format'])) {
109       throw new MigrateException('Format date plugin is missing from_format configuration.');
110     }
111     if (empty($this->configuration['to_format'])) {
112       throw new MigrateException('Format date plugin is missing to_format configuration.');
113     }
114
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;
121     }
122     else {
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;
127     }
128     $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
129
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.
133     try {
134       $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]);
135     }
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);
138     }
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);
141     }
142
143     return $transformed;
144   }
145
146 }