วิธีย้ายเนื้อหาจากไฟล์ XML ไปยัง Drupal 8 (หรือ 9)
เผยแพร่แล้ว: 2021-10-05การดึงข้อมูลจากแหล่งต่าง ๆ ไปยัง Drupal CMS ของคุณคือสิ่งที่เกี่ยวกับการโยกย้าย แหล่งที่มาของการย้ายข้อมูลอาจเป็นอะไรก็ได้ ไม่ว่าจะเป็นฐานข้อมูล ไฟล์ CMS, CSV, XML อื่น Drupal ให้ความยืดหยุ่นแก่คุณในการย้ายข้อมูลจากทุกที่ไปยัง CMS เราได้เขียนเกี่ยวกับการย้ายข้อมูล Drupal อย่างละเอียดก่อน เช่น การย้ายจากแหล่งฐานข้อมูล การย้ายเนื้อหาหลายภาษาจาก CSV จากแหล่ง SQL คู่มือวิธีการที่สมบูรณ์ในการย้ายข้อมูล และอื่นๆ อีกมากมาย! หากคุณกำลังมองหาคำแนะนำในการย้ายข้อมูลจากไฟล์ XML ไปยัง Drupal 8 หรือ Drupal 9 คุณมาถูกที่แล้ว เพราะนั่นคือสิ่งที่เราจะพูดถึงที่นี่!
หากคุณต้องการนำเข้าฟีดภายนอกไปยังเว็บไซต์ Drupal 9 ของคุณโดยใช้โมดูล Drupal Feeds โปรดดูบทความนี้

โมดูลการย้ายข้อมูล Drupal 8/9
ในที่นี้ เราจะใช้โมดูลการย้ายข้อมูล Drupal ทั้งชุด
- โยกย้าย
- โยกย้าย Drupal
- Migrate Plus
- ย้ายเครื่องมือ
หลังจากติดตั้งโมดูลเหล่านี้ คุณจะต้องสร้างโมดูลที่กำหนดเองซึ่งคุณจะเขียนสคริปต์สำหรับการย้ายข้อมูล ขั้นแรก ให้สร้างโมดูลที่กำหนดเองแล้วสร้าง 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 : นี่คือกลุ่มหลักที่มีการโยกย้ายทั้งหมด
- แหล่งที่มา : จะมีรายละเอียดแหล่งที่มาทั้งหมดสำหรับการย้ายข้อมูลนี้
- urls : จะมีไฟล์สำหรับการย้ายข้อมูล ในโค้ดด้านบนนี้ ซอร์สจะอยู่ในไดเร็กทอรีส่วนตัว
- item_selector : ตัวเลือกรายการคือพาธจากชุดข้อมูลทั้งหมด โดยทั่วไปภายในแท็กนี้ ควรมีข้อมูลหนึ่งชุด
- ฟิลด์ : ที่นี่ เรากำลังกำหนดค่าแท็กให้กับตัวแปรบางตัว
- name : ซึ่งให้ชื่อตัวแปร
- ป้ายกำกับ : คำอธิบายสั้น ๆ
- selector : ซึ่งเรากำลังบอกว่าควรกำหนดค่าแท็กใดให้กับตัวแปร
- ids : ซึ่งควรไม่ซ้ำกันสำหรับแต่ละเนื้อหา
- กระบวนการ : ที่เรากำลังทำการแมปชื่อตัวแปรและฟิลด์เครื่อง
- ปลายทาง : ซึ่งเรากำลังกล่าวถึงชื่อเอนทิตีที่ควรย้ายข้อมูล
จากข้อมูลของคุณ คุณสามารถเขียนปลั๊กอินสำหรับการโยกย้ายได้เช่นกัน โค้ดปลั๊กอินด้านบนนี้เขียนขึ้นสำหรับฟิลด์ field_author
หลังจากนี้ไปที่ Structure คุณจะเห็น Migration - คลิกที่มัน ขณะนี้คุณสามารถดูกลุ่มการย้ายข้อมูลทั้งหมดได้แล้ว

คลิกที่ รายการการโยกย้าย คุณสามารถดูรายการการย้ายข้อมูล yaml ที่เราได้สร้างไว้ที่นั่น

คลิกที่ปุ่ม ดำเนินการ คุณจะเห็นตัวเลือกการย้ายข้อมูล

ที่นี่คุณสามารถเรียกใช้ตัวเลือกใดก็ได้ที่คุณต้องการ:
- นำเข้า: การดำเนินการนี้จะย้ายข้อมูล
- ย้อนกลับ: จะย้อนกลับข้อมูลที่ย้ายทั้งหมด
- หยุด: จะหยุดการย้ายข้อมูลในระหว่างกระบวนการ
- รีเซ็ต: จะเปลี่ยนสถานะเป็นสถานะไม่ได้ใช้งาน
คุณยังสามารถย้ายเนื้อหาผ่านเทอร์มินัลด้วยคำสั่ง drush เหล่านี้
- drush ms - สถานะการย้ายข้อมูล
- drush mim <migration_id> - การนำเข้าการย้ายข้อมูล
- drush mr <migration_id> - การย้อนกลับการโยกย้าย
- drush mrs <migration_id> - เพื่อให้สถานะการย้ายไม่ได้ใช้งาน