Commit 0ab00b4a authored by catch's avatar catch

Issue #2893072 by maxocub: Modify the DateField Field plugin so it's used for D6 & D7

parent 3e6a3f09
<?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);
}
}
......@@ -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 {
......
<?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']);
}
}
......@@ -8,6 +8,7 @@
/**
* @group migrate
* @group legacy
*/
class DateFieldTest extends UnitTestCase {
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment