كيفية ترحيل الفقرات من Drupal 7 إلى Drupal 8
قبل بضعة أيام، واجهنا تحديًا مثيرًا: ترحيل الفقرات من منصة Drupal 7 إلى Drupal 8. أثناء البحث عن الحل المناسب، بحثنا في الويب ولكننا لم نجد شيئًا يناسب احتياجاتنا تمامًا. ونتيجة لذلك، قام مطورو الخلفية لدينا بإنشاء حل مخصص. شكر خاص لـ mtech-llc.com وأدا هيرنانديز، الذين ألهمت أعمالهم حول ترحيل مجموعة الحقول هذا الحل.
بينما يصف المقال الأصلي لأدا ترحيل مجموعات الحقول، قمنا بتكييف النهج لترحيل الفقرات. أثبتت رؤاها أنها لا تقدر بثمن في عمليتنا.
قبل المتابعة، تأكد من أن لديك:
1. تثبيت Drupal 8 يعمل بشكل صحيح
2. تثبيت الوحدات التالية:
- migrate_drupal
- migrate_plus
- migrate_tools
- migrate_drupal_ui
بدلاً من ذلك، يمكنك إضافة هذه كتبعيات لوحدتك المخصصة.
بعد تثبيت الوحدات المطلوبة، قم بإنشاء فقرة بحزمة "contact" تحتوي على هذه الحقول:
- نص عادي (اسم الآلة: field_name)
- البريد الإلكتروني (اسم الآلة: field_email)
- رقم (صحيح) (اسم الآلة: field_phone)
ثم قم بإنشاء نوع محتوى يسمى Organization (اسم الآلة: organization) مع:
- حقل الجسم الافتراضي
- نوع حقل الفقرة (اسم الآلة: field_contact)
هذه الإجراءات معقدة وتتطلب الاهتمام الدقيق بالتفاصيل. إذا حدثت أخطاء خلال أي خطوة، قد تحتاج إلى البدء من جديد. يرجى قراءة كل خطوة بعناية قبل المتابعة.
1. حدد اتصال قاعدة البيانات في settings.php
أضف الاتصال الثاني إلى Drupal 7 في ملف settings.php الخاص بك. في هذا المثال، اسم قاعدة البيانات هو "drupal_7_56":
$databases['migrate']['default'] = array(
'database' => 'drupal_7_56',
'username' => 'root',
'password' => '',
'prefix' => '',
'host' => '127.0.0.1',
'port' => '33067',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
2. إنشاء وحدة مخصصة
قم بإنشاء وحدة مخصصة بالاسم الآلي "custom".
3. إنشاء قوالب YML
ضع القوالب YML التالية في مجلد config/install:
أولاً، قم بإنشاء قالب الترحيل للفقرة (اسم الملف: migrate_plus.migration.d7_paragraph_contact.yml):
langcode: en
status: true
dependencies: {}
id: d7_paragraph_contact
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
- 'Drupal 7'
migration_group: migrate_drupal_7
label: Contacts
source:
plugin: d7_paragraph_item
key: migrate
field_name: field_contact
process:
field_name:
plugin: iterator
source: field_name
process:
value: value
revision_id: revision_id
field_email:
plugin: iterator
source: field_email
process:
value: email
revision_id: revision_id
field_phone:
plugin: iterator
source: field_phone
process:
value: value
revision_id: revision_id
destination:
plugin: 'entity_reference_revisions:paragraph'
default_bundle: contact
migration_dependencies:
required: {}
optional: {}
ثم قم بإنشاء قالب الترحيل للعقدة (اسم الملف: migrate_plus.migration.d7_node_organization.yml):
langcode: en
status: true
dependencies: {}
id: d7_node_organization
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
- 'Drupal 7'
- Content
migration_group: migrate_drupal_7
label: 'Nodes (Organization)'
source:
plugin: d7_node
node_type: organization
process:
nid: nid
vid: vid
langcode:
plugin: default_value
source: language
default_value: und
title: title
uid: node_uid
status: status
created: created
changed: changed
promote: promote
sticky: sticky
revision_uid: revision_uid
revision_log: log
revision_timestamp: timestamp
body:
plugin: iterator
source: body
process:
value: value
format:
- plugin: static_map
bypass: true
source: format
map:
- null
- plugin: skip_on_empty
method: process
- plugin: migration
migration:
- d6_filter_format
- d7_filter_format
source: format
field_contact:
- plugin: skip_on_empty
method: process
source: field_contact
- plugin: migration_lookup
migration: d7_paragraph_contact
no_stub: true
- plugin: iterator
process:
target_id: '0'
target_revision_id: '1'
destination:
plugin: 'entity:node'
default_bundle: organization
migration_dependencies:
required: {}
optional: {}
4. إنشاء فئة البرنامج المساعد المصدر
قم بإنشاء ملف باسم ContactParagraph.php في الدليل /src/Plugin/migrate/source:
<?php
namespace Drupal\mparagraf\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* D7_paragraph_item source.
*
* @MigrateSource(
* id = "d7_paragraph_item"
* )
*/
class ContactParagraph extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
// Select node in its last revision.
$query = $this->select('paragraphs_item', 'fci')
->fields('fci', [
'item_id',
'field_name',
'revision_id',
]);
if (isset($this->configuration['field_name'])) {
$query->innerJoin('field_data_' . $this->configuration['field_name'], 'fd', 'fd.' . $this->configuration['field_name'] . '_value = fci.item_id');
$query->fields('fd', [
'entity_type',
'bundle',
'entity_id',
$this->configuration['field_name'] . '_revision_id',
]);
$query->condition('fci.field_name', $this->configuration['field_name']);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
// If field specified, get field revision ID so there aren't issues mapping.
if (isset($this->configuration['field_name'])) {
$row->setSourceProperty('revision_id', $row->getSourceProperty($this->configuration['field_name'] . '_revision_id'));
}
// Get field API field values.
foreach (array_keys($this->getFields('paragraphs_item', 'contact')) as $field) {
$item_id = $row->getSourceProperty('item_id');
$revision_id = $row->getSourceProperty('revision_id');
$row->setSourceProperty($field, $this->getFieldValues('paragraphs_item', $field, $item_id, $revision_id));
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'item_id' => $this->t('Item ID'),
'revision_id' => $this->t('Revision ID'),
'field_name' => $this->t('Name of field'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['item_id']['type'] = 'integer';
$ids['item_id']['alias'] = 'fci';
return $ids;
}
}
5. تنفيذ خطاف الترحيل
أضف الكود التالي إلى ملف .module الخاص بالوحدة:
<?php
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Row;
/**
* Implements hook_migrate_MIGRATION_ID_prepare_row().
*/
function custom_migrate_d7_node_organization_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
$values = $row->getSourceProperty('field_contact');
$value_new = [];
if ($values) {
foreach ($values as $value) {
$value_new[] = ['item_id' => $value['value']];
}
$row->setSourceProperty('field_contact', $value_new);
}
}
6. الخطوات النهائية
بمجرد إكمال كل الخطوات السابقة بنجاح، يمكنك تشغيل الترحيل ومشاهدة كيف يتم نقل الفقرات من Drupal 7 إلى Drupal 8. خذ لحظة للتحقق من النتائج قبل المتابعة مع أي مهام ذات صلة بالفقرات.
تذكر أن تختبر بشكل شامل في بيئة التطوير قبل محاولة هذا الترحيل على موقع الإنتاج.
