From 0ab00b4acd49624c93bb7ff26c14a394d7fb44b2 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Sat, 5 Aug 2017 16:09:08 +0900 Subject: [PATCH] Issue #2893072 by maxocub: Modify the DateField Field plugin so it's used for D6 & D7 --- .../src/Plugin/migrate/field/DateField.php | 79 +++++++++++++++++++ .../src/Plugin/migrate/field/d6/DateField.php | 5 ++ .../Plugin/migrate/field/DateFieldTest.php | 35 ++++++++ .../Plugin/migrate/field/d6/DateFieldTest.php | 1 + .../Migrate/d7/MigrateFieldInstanceTest.php | 2 +- .../Kernel/Migrate/d7/MigrateFieldTest.php | 2 +- 6 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 core/modules/datetime/src/Plugin/migrate/field/DateField.php create mode 100644 core/modules/datetime/tests/src/Unit/Plugin/migrate/field/DateFieldTest.php 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 000000000000..3a61a7846ad5 --- /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 60b1c68c8306..a2429f6a01b2 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 000000000000..6b7f13727e16 --- /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 b8b545af805a..7e6788110e90 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 1e3df9e4fc8b..ef12ac161771 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 ab2fe8e12589..c0af33898a50 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); -- GitLab