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: String identifying the required time zone, see
19 * DateTimePlus::__construct().
20 * - settings: keyed array of settings, see DateTimePlus::__construct().
24 * Example usage for date only fields (DATETIME_DATE_STORAGE_FORMAT):
29 * from_format: 'm/d/Y'
34 * If the source value was '01/05/1955' the transformed value would be
37 * Example usage for datetime fields (DATETIME_DATETIME_STORAGE_FORMAT):
42 * from_format: 'm/d/Y H:i:s'
43 * to_format: 'Y-m-d\TH:i:s'
47 * If the source value was '01/05/1955 10:43:22' the transformed value would be
48 * 1955-01-05T10:43:22.
50 * Example usage for datetime fields with a timezone and settings:
55 * from_format: 'Y-m-d\TH:i:sO'
56 * to_format: 'Y-m-d\TH:i:s'
57 * timezone: 'America/Managua'
59 * validate_format: false
63 * If the source value was '2004-12-19T10:19:42-0600' the transformed value
64 * would be 2004-12-19T10:19:42.
66 * @see \DateTime::createFromFormat()
67 * @see \Drupal\Component\Datetime\DateTimePlus::__construct()
68 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
70 * @MigrateProcessPlugin(
74 class FormatDate extends ProcessPluginBase {
79 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
84 // Validate the configuration.
85 if (empty($this->configuration['from_format'])) {
86 throw new MigrateException('Format date plugin is missing from_format configuration.');
88 if (empty($this->configuration['to_format'])) {
89 throw new MigrateException('Format date plugin is missing to_format configuration.');
92 $fromFormat = $this->configuration['from_format'];
93 $toFormat = $this->configuration['to_format'];
94 $timezone = isset($this->configuration['timezone']) ? $this->configuration['timezone'] : NULL;
95 $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : [];
97 // Attempts to transform the supplied date using the defined input format.
98 // DateTimePlus::createFromFormat can throw exceptions, so we need to
99 // explicitly check for problems.
101 $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $timezone, $settings)->format($toFormat);
103 catch (\InvalidArgumentException $e) {
104 throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);
106 catch (\UnexpectedValueException $e) {
107 throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e);