遷移思路
在 Drupal 8 環境下本身寫 Sql 把 Drupal 7 下的數據所有查出來,再組織好數據結構調用 Drupal 8 API 將數據插入到 Drupal 8 數據庫中php
遷移步驟 node
修改 Drupal 8 站點的 setting.php 文件mysql
// 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', );
$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');
$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 web
若是是附件或者圖片的話這個沒有嘗試,能夠自行研究。sql