You are here

public function FormatDate::transform in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/process/FormatDate.php \Drupal\migrate\Plugin\migrate\process\FormatDate::transform()
  2. 9 core/modules/migrate/src/Plugin/migrate/process/FormatDate.php \Drupal\migrate\Plugin\migrate\process\FormatDate::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

mixed The newly transformed value.

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/FormatDate.php, line 98

Class

FormatDate
Converts date/datetime from one format to another.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (empty($value) && $value !== '0' && $value !== 0) {
    return '';
  }

  // Validate the configuration.
  if (empty($this->configuration['from_format'])) {
    throw new MigrateException('Format date plugin is missing from_format configuration.');
  }
  if (empty($this->configuration['to_format'])) {
    throw new MigrateException('Format date plugin is missing to_format configuration.');
  }
  $fromFormat = $this->configuration['from_format'];
  $toFormat = $this->configuration['to_format'];
  $system_timezone = date_default_timezone_get();
  $default_timezone = !empty($system_timezone) ? $system_timezone : 'UTC';
  $from_timezone = $this->configuration['from_timezone'] ?? $default_timezone;
  $to_timezone = $this->configuration['to_timezone'] ?? $default_timezone;
  $settings = $this->configuration['settings'] ?? [];

  // Older versions of Drupal where omitting certain granularity values (also
  // known as "collected date attributes") resulted in invalid timestamps
  // getting stored.
  if ($fromFormat === 'Y-m-d\\TH:i:s') {
    $value = str_replace([
      '-00-00T',
      '-00T',
    ], [
      '-01-01T',
      '-01T',
    ], $value);
  }

  // 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, $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);
  } catch (\UnexpectedValueException $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);
  }
  return $transformed;
}