如何將內容從 XML 文件遷移到 Drupal 8(或 9)

已發表: 2021-10-05

將不同來源的數據提取到您的 Drupal CMS 就是遷移的全部內容。 您的遷移源可以是任何東西——數據庫、另一個 CMS、CSV、XML 文件。 Drupal 始終為您提供將數據從任何地方遷移到 CMS 的靈活性。 在從數據庫源遷移、從 CSV、從 SQL 源遷移多語言內容、完整的遷移指南等等之前,我們已經寫了大量關於 Drupal 遷移的文章! 如果您正在尋找將數據從 XML 文件遷移到 Drupal 8 或 Drupal 9 的指南,那麼您來對地方了,因為這就是我們將在這裡討論的內容!

如果您希望使用 Drupal 訂閱模塊將外部訂閱源導入您的 Drupal 9 網站,請查看這篇文章。

XML 到 Drupal 的遷移

Drupal 8/9 遷移模塊

在這裡,我們將使用整個 Drupal 遷移模塊

  • 遷移
  • 遷移 Drupal
  • 遷移加
  • 遷移工具

安裝這些模塊後,您將需要創建一個自定義模塊,您將在其中編寫遷移腳本。 因此,首先,創建一個自定義模塊,然後創建一個包含模塊詳細信息的 info.yml。 接下來,啟用模塊。

 name: test migration type: module description: Migrating data from the csv files. core_version_requirement: ^8 || ^9 package: Migration dependencies: - drupal:migrate_source_csv - drupal:migrate_plus - drupal:migrate_tools config_devel: install: - migrate_plus.migration_group.test_migration - migrate_plus.migration.test_migration_sessions - migrate_plus.migration.test_migration_paper - migrate_plus.migration.test_migration_user

創建 info.yml 文件後,您需要為此遷移創建一個遷移組。 您需要在以下路徑中創建此遷移組: test_migration> config > install 。 運行遷移時需要這些詳細信息:

 id: test_migration label: test Migration description: Migrating xml data

創建遷移組後,將此組安裝在您的 info.yml 文件中。

 config_devel: install: - migrate_plus.migration_group.test_migration

現在,您需要為內容類型編寫遷移腳本。 這個腳本應該在 config > install 裡面,文件名應該是 migrate_plus.migration.test_migration_paper.yml

 id: test_migration_paper label: 'Migrate Paper data from the xml file' migration_group: test_migration source: plugin: url # Full path to the file. data_fetcher_plugin: file data_parser_plugin: xml urls: private://xml/session.xml item_selector: /program/session/papers/paper fields: - name : title lable : 'Paper title' selector : papertitle - name : abstract lable : 'Paper abstract' selector : abstract - name : first_name label : 'Author first name' selector : authors/author/name/givenname - name : last_name label : 'Author last name' selector : authors/author/name/surname - name : paper_id label : 'Paper identifier' selector : paperid - name : session_id label : 'Session identifier' selector : sessionid - name : start_time label : 'Paper presentation start time' selector : starttime - name : end_time label : 'Paper presentation end time' selector : endtime - name : author_name label : 'Author name' selector : authors/author/name/fullname - name : session lable : 'Session name' selector : session ids: session_id : type : string process : # Adding the mapping between the fields and the csv columns. title : - plugin : skip_on_empty method : process source : title field_abstract/value : abstract field_abstract/format : plugin : default_value default_value : "full_html" field_presentation_timing/value : plugin : format_date from_format : 'Ymd\TH:i:s' to_format : 'Ymd\TH:i:s' source : start_time field_presentation_timing/end_value : plugin : format_date from_format : 'Ymd\TH:i:s' to_format : 'Ymd\TH:i:s' source : end_time field_paper_identifier : paper_id field_session_identifier : session_id field_author : - plugin : skip_on_empty method : process source : author_name - plugin : paper_user_migration_import_process no_stub : true field_session : - plugin : skip_on_empty method : process source : session - plugin : node_migration_import_process no_stub : true destination : plugin : 'entity:node' default_bundle : paper migration_dependencies : required : { } dependencies : { }
廣告標籤

  • id對於每個yaml文件都是唯一的,並顯示在遷移列表中。
  • label將包含遷移的描述。
  • migration_group這是包含所有遷移的主要組。
  • source將包含此遷移的所有源詳細信息。
  • urls將包含用於遷移的文件。 在上面的代碼中,源代碼放在私有目錄中。
  • item_selector項目選擇器是獲取整個數據集的路徑。 基本上在這個標籤裡面應該有一組數據。
  • fields這裡我們將標籤值分配給一些變量。
  • name它給出了變量名。
  • 標籤簡短描述。
  • selector我們在其中告訴變量應該分配哪個標籤值。
  • ids每個內容應該是唯一的。
  • process我們在其中映射變量和字段機器名稱。
  • destination我們在其中提到數據應遷移到的實體名稱。

根據您的數據,您現在也可以編寫用於遷移的插件。 上面的代碼插件是為field_author字段編寫的。

在此之後轉到Structure ,您將在那裡看到Migration - 單擊它。 您現在可以看到所有遷移組。

添加遷移組

單擊列表遷移。 在那裡你可以看到我們創建的遷移yaml列表。

論文數據

單擊執行按鈕。 您可以看到遷移選項。

導入操作

在這裡你可以運行任何你想要的選項:

  • 導入:這將遷移數據。
  • 回滾:將回滾所有遷移的數據。
  • 停止:將在過程中間停止遷移。
  • 重置:將狀態更改為空閒狀態。

您還可以使用這些 drush 命令通過終端遷移內容。

  • drush ms - 遷移狀態
  • drush mim <migration_id> - 遷移導入
  • drush mr <migration_id> - 遷移回滾
  • drush mrs <migration_id> - 使遷移狀態空閒。