遷移思路
在 Drupal 8 環境下自己寫 Sql 把 Drupal 7 下的數據全部查出來,再組織好數據結構調用 Drupal 8 API 將數據插入到 Drupal 8 數據庫中
遷移步驟
- 配置 Database 連接環境
修改 Drupal 8 站點的 setting.php 文件
// Drupal 8 數據庫鏈接
$databases['default']['default'] = array (
'database' => 'd8',
'username' => 'root',
'password' => '',
'prefix' => '',
'host' => '127.0.0.1',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
// Drupal 7 數據庫鏈接
$databases['upgrade']['default'] = array (
'database' => 'd7',
'username' => 'root',
'password' => '',
'prefix' => '',
'host' => '127.0.0.1',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
- 根據 Drupal 7 Webform 結構,在 Drupal 8 中創建同樣的 Webform
- 查出 Drupal 7 Webform 數據
$nid = 1382; // drupal7 webform 對應的 node Id
$firstSid = 0;
$webformId = 'register_as_an_independent';
// 獲取 Drupal7 表單的 submission
$submittedData = [];
$dataQuery = Database::getConnection('default', 'upgrade')->select('webform_submitted_data', 'wd');
$dataQuery->join('webform_component', 'c', 'c.cid = wd.cid AND c.nid = wd.nid');
$dataQuery->addField('c', 'form_key');
$dataQuery->addField('wd', 'sid');
$dataQuery->addField('wd', 'data');
$dataQuery->condition('wd.nid', $nid, '=');
$result = $dataQuery->execute()->fetchAll();
foreach ($result as $row) {
$submittedData[$row->sid][$row->form_key][] = [
'value' => $row->data,
];
}
$query = Database::getConnection('default', 'upgrade')->select('webform_submissions', 'ws');
$query->addField('ws', 'sid');
$query->addField('ws','uid');
$query->addField('ws','submitted');
$query->addField('ws','completed');
$query->addField('ws','modified');
$query->addField('ws','remote_addr');
$query->condition('nid', $nid, '=');
$query->condition('sid', $firstSid, '>');
// $query->condition('sid', ($firstSid+9), '<');
$submit = $query->execute()->fetchAllAssoc('sid');
- 將數據導入到 Drupal 8 裏面
$entityId = 90; // Drupal 8 中表單對應的 node ID
$checkBoxKey = array('target_types','following_dbms','versions_of_powerbuilder','work_locations');
foreach ($submit as $sid => $info) {
if (empty($submittedData[$sid])) {
continue;
}
// 導入數據到 D8c
$values = [
'webform_id' => $webformId,
'entity_type' => 'node',
'entity_id' => $entityId,
'in_draft' => FALSE,
'uid' => $info->uid,
'langcode' => 'en',
'token' => 'pgmJREX2l4geg2RGFp0p78Qdfm1ksLxe6IlZ-mN9GZI',
'uri' => '/consultants/independents/apply',
'created' => $info->submitted,
'completed' => $info->completed,
'changed' => $info->modified,
'remote_addr' => $info->remote_addr,
'data' => [
'target_types' => ['Appeon Web App'],
'following_dbms' => ['SQL Server'],
'versions_of_powerbuilder' => ['PB 12.x'],
'years_using_powerbuilder' => '5',
'powerbuilder_certification' => 'No',
'id_number' => '',
'linkedin_or_xing_profile' => '',
'work_type' => 'Full-time',
'work_locations' => ['Telecommute'],
'first_name' => 'first_name',
'last_name' => 'last_name',
'email_address' => '@',
'country' => 'US',
'state_province' => '',
'postal_code' => '0000',
'certify_true_and_correct' => '1',
'agree_to_be_published' => '1',
'agree_terms' => '1',
'email_of_subject' => '',
],
];
foreach ($submittedData[$sid] as $key => $value){
if (in_array($key,$checkBoxKey)){
$values['data'][$key] = [];
}
foreach ($value as $val){
if (in_array($key,$checkBoxKey)){
$values['data'][$key][] = $val['value'];
} else {
$values['data'][$key] = $val['value'];
}
}
}
// Check webform is open.
$webform = Webform::load($values['webform_id']);
$is_open = WebformSubmissionForm::isOpen($webform);
if ($is_open === TRUE) {
// Create new submission instance
$webform_submission = WebformSubmission::create($values);
// Validate submission.
$errors = WebformSubmissionForm::validateWebformSubmission($webform_submission);
// Check there are no validation errors.
if (!empty($errors)) {
print_r($errors);
die();
}
else {
// Submit values and get submission ID.
$webform_submission = WebformSubmissionForm::submitWebformSubmission($webform_submission);
print $webform_submission->id();
}
}
}
注意:
字段如果是多選的話,插入字段值應該是個 array
如果是附件或者圖片的話這個沒有嘗試,可以自行研究。