Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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.
86  *
87  * @see \DateTime::createFromFormat()
88  * @see \Drupal\Component\Datetime\DateTimePlus::__construct()
89  * @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface
90  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
91  *
92  * @MigrateProcessPlugin(
93  *   id = "format_date"
94  * )
95  */
96 class FormatDate extends ProcessPluginBase {
97
98   /**
99    * {@inheritdoc}
100    */
101   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
102     if (empty($value)) {
103       return '';
104     }
105
106     // Validate the configuration.
107     if (empty($this->configuration['from_format'])) {
108       throw new MigrateException('Format date plugin is missing from_format configuration.');
109     }
110     if (empty($this->configuration['to_format'])) {
111       throw new MigrateException('Format date plugin is missing to_format configuration.');
112     }
113
114     $fromFormat = $this->configuration['from_format'];
115     $toFormat = $this->configuration['to_format'];
116     if (isset($this->configuration['timezone'])) {
117       @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);
118       $from_timezone = $this->configuration['timezone'];
119       $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : NULL;
120     }
121     else {
122       $system_timezone = date_default_timezone_get();
123       $default_timezone = !empty($system_timezone) ? $system_timezone : 'UTC';
124       $from_timezone = isset($this->configuration['from_timezone']) ? $this->configuration['from_timezone'] : $default_timezone;
125       $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : $default_timezone;
126     }
127     $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
128
129     // Attempts to transform the supplied date using the defined input format.
130     // DateTimePlus::createFromFormat can throw exceptions, so we need to
131     // explicitly check for problems.
132     try {
133       $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]);
134     }
135     catch (\InvalidArgumentException $e) {
136       throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
137     }
138     catch (\UnexpectedValueException $e) {
139       throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
140     }
141
142     return $transformed;
143   }
144
145 }