Unverified Commit 4e87f84c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3095237 by quietone, ravi.shankar, Vishalghyv, yogeshmpawar,...

Issue #3095237 by quietone, ravi.shankar, Vishalghyv, yogeshmpawar, benjifisher, Wim Leers, Matroskeen, heddn, mikelutz: Migrate Drupal 7 date field "todate" value
parent 91fdb8aa
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ class MigrateCommentTest extends MigrateDrupal7TestBase {
    'comment',
    'content_translation',
    'datetime',
    'datetime_range',
    'filter',
    'image',
    'language',
+32 −0
Original line number Diff line number Diff line
@@ -5,8 +5,11 @@
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;

// cspell:ignore todate

/**
 * Provides a field plugin for date and time fields.
 *
@@ -92,6 +95,15 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field
      ],
    ];

    // If the 'todate' setting is specified the field is now a 'daterange' and
    // so set the end value. If the datetime_range module is not enabled on the
    // destination then end_value is ignored and a message is logged in the
    // relevant migrate message table.
    if (!empty($field_data['settings']['todate'])) {
      $process['end_value'] = $process['value'];
      $process['end_value']['source'] = 'value2';
    }

    $process = [
      'plugin' => 'sub_process',
      'source' => $field_name,
@@ -100,4 +112,24 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field
    $migration->mergeProcessOfProperty($field_name, $process);
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldType(Row $row) {
    $field_type = parent::getFieldType($row);

    // If the 'todate' setting is specified then change the field type to
    // 'daterange' so we can migrate the end date.
    if ($field_type === 'datetime' && !empty($row->get('settings/todate'))) {
      if (\Drupal::service('module_handler')->moduleExists('datetime_range')) {
        return 'daterange';
      }
      else {
        throw new MigrateException(sprintf("Can't migrate field '%s' with 'todate' settings. Enable the datetime_range module. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#datetime", $row->get('field_name')));
      }
    }

    return $field_type;
  }

}
+49 −12
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
use Drupal\migrate\MigrateException;
use Drupal\Tests\UnitTestCase;

// cspell:ignore todate

/**
 * Provides unit tests for the DateField Plugin.
 *
@@ -24,9 +26,7 @@ class DateFieldTest extends UnitTestCase {
   */
  public function testDefineValueProcessPipeline($data, $from_format, $to_format) {
    $migration = $this->createMock('Drupal\migrate\Plugin\MigrationInterface');
    $migration->expects($this->once())
      ->method('mergeProcessOfProperty')
      ->with('field_date', [
    $pipeline = [
      'plugin' => 'sub_process',
      'source' => 'field_date',
      'process' => [
@@ -37,7 +37,24 @@ public function testDefineValueProcessPipeline($data, $from_format, $to_format)
          'source' => 'value',
        ],
      ],
      ])
    ];

    // If there is a todate then add a process for the end value.
    if (isset($data['field_definition']['data'])) {
      $tmp = is_string($data['field_definition']['data']) ? unserialize($data['field_definition']['data']) : '';
      $todate = $tmp['settings']['todate'] ?? NULL;
      if (!empty($todate)) {
        $pipeline['process']['end_value'] = [
          'plugin' => 'format_date',
          'from_format' => $from_format,
          'to_format' => $to_format,
          'source' => 'value2',
        ];
      }
    }
    $migration->expects($this->once())
      ->method('mergeProcessOfProperty')
      ->with('field_date', $pipeline)
      ->will($this->returnValue($migration));

    $plugin = new DateField([], '', []);
@@ -80,6 +97,7 @@ public function providerTestDefineValueProcessPipeline() {
                  0 => 'year',
                  1 => 'month',
                ],
                'todate' => '',
              ],
            ]),
          ],
@@ -87,6 +105,25 @@ public function providerTestDefineValueProcessPipeline() {
        'Y-m-d\TH:i:s',
        'Y-m-d',
      ],
      'datetime with a todate' => [
        [
          'type' => 'datetime',
          'field_definition' => [
            'data' => serialize([
              'settings' => [
                'granularity' => [
                  'hour' => 0,
                  'minute' => 0,
                  'second' => 0,
                ],
                'todate' => 'optional',
              ],
            ]),
          ],
        ],
        'Y-m-d H:i:s',
        'Y-m-d',
      ],
    ];
  }

+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ class MigrateFieldFormatterSettingsTest extends MigrateDrupal7TestBase {
  protected static $modules = [
    'comment',
    'datetime',
    'datetime_range',
    'image',
    'link',
    'menu_ui',
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ class MigrateFieldInstanceLabelDescriptionTest extends MigrateDrupal7TestBase im
    'comment',
    'config_translation',
    'datetime',
    'datetime_range',
    'field',
    'file',
    'image',
Loading