diff --git a/core/modules/datetime/src/Plugin/migrate/field/DateField.php b/core/modules/datetime/src/Plugin/migrate/field/DateField.php new file mode 100644 index 0000000000000000000000000000000000000000..3a61a7846ad5ad75d5e8397c9f214b29af9f458c --- /dev/null +++ b/core/modules/datetime/src/Plugin/migrate/field/DateField.php @@ -0,0 +1,79 @@ +<?php + +namespace Drupal\datetime\Plugin\migrate\field; + +use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\MigrateException; +use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase; + +/** + * @MigrateField( + * id = "datetime", + * type_map = { + * "date" = "datetime", + * "datestamp" = "timestamp", + * "datetime" = "datetime", + * }, + * core = {6,7} + * ) + */ +class DateField extends FieldPluginBase { + + /** + * {@inheritdoc} + */ + public function getFieldWidgetMap() { + return [ + 'date' => 'datetime_default', + 'datetime' => 'datetime_default', + 'datestamp' => 'datetime_timestamp', + ]; + } + + /** + * {@inheritdoc} + */ + public function getFieldFormatterMap() { + // See d6_field_formatter_settings.yml, d7_field_formatter_settings.yml and + // FieldPluginBase::processFieldFormatter(). + return []; + } + + /** + * {@inheritdoc} + */ + public function processFieldValues(MigrationInterface $migration, $field_name, $data) { + switch ($data['type']) { + case 'date': + $from_format = 'Y-m-d\TH:i:s'; + $to_format = 'Y-m-d\TH:i:s'; + break; + case 'datestamp': + $from_format = 'U'; + $to_format = 'U'; + break; + case 'datetime': + $from_format = 'Y-m-d H:i:s'; + $to_format = 'Y-m-d\TH:i:s'; + break; + default: + throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_name, var_export($data['type'], TRUE))); + } + $process = [ + 'value' => [ + 'plugin' => 'format_date', + 'from_format' => $from_format, + 'to_format' => $to_format, + 'source' => 'value', + ], + ]; + + $process = [ + 'plugin' => 'iterator', + 'source' => $field_name, + 'process' => $process, + ]; + $migration->mergeProcessOfProperty($field_name, $process); + } + +} diff --git a/core/modules/datetime/src/Plugin/migrate/field/d6/DateField.php b/core/modules/datetime/src/Plugin/migrate/field/d6/DateField.php index 60b1c68c830678d48245887b2302e9e63dc26abb..a2429f6a01b2230a7bb4d0bb677912d2593c25e5 100644 --- a/core/modules/datetime/src/Plugin/migrate/field/d6/DateField.php +++ b/core/modules/datetime/src/Plugin/migrate/field/d6/DateField.php @@ -2,6 +2,8 @@ namespace Drupal\datetime\Plugin\migrate\field\d6; +@trigger_error('DateField is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\migrate\field\DateField instead.', E_USER_DEPRECATED); + use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\MigrateException; use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase; @@ -16,6 +18,9 @@ * }, * core = {6} * ) + * + * @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use + * \Drupal\datetime\Plugin\migrate\field\DateField instead. */ class DateField extends FieldPluginBase { diff --git a/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/DateFieldTest.php b/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/DateFieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6b7f13727e1638d5e8f674a933065010861f0b34 --- /dev/null +++ b/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/DateFieldTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field; + +use Drupal\datetime\Plugin\migrate\field\DateField; +use Drupal\migrate\MigrateException; +use Drupal\Tests\UnitTestCase; + +/** + * @group migrate + */ +class DateFieldTest extends UnitTestCase { + + /** + * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface + */ + protected $plugin; + + /** + * @var \Drupal\migrate\Plugin\MigrationInterface + */ + protected $migration; + + /** + * Tests an Exception is thrown when the field type is not a known date type. + */ + public function testUnknownDateType() { + $this->migration = $this->prophesize('Drupal\migrate\Plugin\MigrationInterface')->reveal(); + $this->plugin = new DateField([], '', []); + + $this->setExpectedException(MigrateException::class, "Field field_date of type 'timestamp' is an unknown date field type."); + $this->plugin->processFieldValues($this->migration, 'field_date', ['type' => 'timestamp']); + } + +} diff --git a/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/d6/DateFieldTest.php b/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/d6/DateFieldTest.php index b8b545af805a250a83b342fbafa6217747c65754..7e6788110e900abe36b5ba581ac82b20a2e1e391 100644 --- a/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/d6/DateFieldTest.php +++ b/core/modules/datetime/tests/src/Unit/Plugin/migrate/field/d6/DateFieldTest.php @@ -8,6 +8,7 @@ /** * @group migrate + * @group legacy */ class DateFieldTest extends UnitTestCase { diff --git a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php index 1e3df9e4fc8b01ba371ada7ba76af260460dbcb3..ef12ac1617718c7f6c94c179c0f95cfb6b7f5527 100644 --- a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php +++ b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldInstanceTest.php @@ -134,7 +134,7 @@ public function testFieldInstances() { $this->assertEntity('node.test_content_type.field_email', 'Email', 'email', FALSE, FALSE); $this->assertEntity('node.test_content_type.field_phone', 'Phone', 'telephone', TRUE, FALSE); $this->assertEntity('node.test_content_type.field_date', 'Date', 'datetime', FALSE, FALSE); - $this->assertEntity('node.test_content_type.field_date_with_end_time', 'Date With End Time', 'datetime', FALSE, FALSE); + $this->assertEntity('node.test_content_type.field_date_with_end_time', 'Date With End Time', 'timestamp', FALSE, FALSE); $this->assertEntity('node.test_content_type.field_file', 'File', 'file', FALSE, FALSE); $this->assertEntity('node.test_content_type.field_float', 'Float', 'float', FALSE, FALSE); $this->assertEntity('node.test_content_type.field_images', 'Images', 'image', TRUE, FALSE); diff --git a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldTest.php b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldTest.php index ab2fe8e12589f6bb8f1aca6845b4b7564a6781a6..c0af33898a506d62444f703a9690a1d5734db347 100644 --- a/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldTest.php +++ b/core/modules/field/tests/src/Kernel/Migrate/d7/MigrateFieldTest.php @@ -97,7 +97,7 @@ public function testFields() { $this->assertEntity('node.field_email', 'email', TRUE, -1); $this->assertEntity('node.field_phone', 'telephone', TRUE, 1); $this->assertEntity('node.field_date', 'datetime', TRUE, 1); - $this->assertEntity('node.field_date_with_end_time', 'datetime', TRUE, 1); + $this->assertEntity('node.field_date_with_end_time', 'timestamp', TRUE, 1); $this->assertEntity('node.field_node_entityreference', 'entity_reference', TRUE, -1); $this->assertEntity('node.field_user_entityreference', 'entity_reference', TRUE, 1); $this->assertEntity('node.field_term_entityreference', 'entity_reference', TRUE, -1);