Uncategorized

Drupal 8 Migrations, part 4: Migrating Nodes from Drupal 7

Note: Now updated for Drupal 8.2!

Drupal 8 provides a flexible, plugin-based architecture for migrating data into a site. In Part 3 of this series, we explored how to migrate taxonomies from a Drupal 7 site. We will now expand on this by migrating basic nodes from a Drupal 7 site into Drupal 8.

The code examples in this post build on the migration module begun in Part 2 of this series. If you are trying this code out yourself, it is recommended to start building your custom migration module according to the examples in that post.

The game plan for migrating nodes
Because Drupal nodes can be of many types and have many different user-defined fields, it is complicated to write a single migration script that can handle all fields for all node types. To keep things simple, we will only migrate the built-in “Article” content type, which has the same default fields in Drupal 7 and Drupal 8.

The migration definition
Starting with the “Migrate Custom” module we created in Part 2, we now add the following configuration file.

modules/migrate_custom/config/install/migrate_plus.migration.custom_article.yml

id: custom_article
label: Custom article node migration from Drupal 7
migration_group: custom
dependencies:
enforced:
module:
– migrate_custom
source:
plugin: custom_article
destination:
plugin: entity:node
bundle: article
process:
nid: nid
vid: vid
type: type
langcode:
plugin: static_map
bypass: true
source: language
map:
und: en
title: title
uid: uid
status: status
created: created
changed: changed
promote: promote
sticky: sticky
‘body/format’:
plugin: static_map
bypass: true
source: body_format
map:
1: plain_text
2: restricted_html
3: full_html
4: full_html
‘body/value’: body_value
‘body/summary’: body_summary
field_tags: tags
field_image: images

The source plugin
Similar to our Users source plugin in Part 2 of this series, our Blog source definition needs to implement both the query() and processRow() methods. We will do this in the following file:

modules/migrate_custom/src/Plugin/migrate/source/Article.php

->condition(‘n.type’, ‘article’)
->fields(‘n’, array(
‘nid’,
‘vid’,
‘type’,
‘language’,
‘title’,
‘uid’,
‘status’,
‘created’,
‘changed’,
‘promote’,
‘sticky’,
));
$query->orderBy(‘nid’);
return $query;
}

/**
* {@inheritdoc}
*/
public function fields() {
$fields = $this->baseFields();
$fields[‘body/format’] = $this->t(‘Format of body’);
$fields[‘body/value’] = $this->t(‘Full text of body’);
$fields[‘body/summary’] = $this->t(‘Summary of body’);
return $fields;
}

/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$nid = $row->getSourceProperty(‘nid’);

// body (compound field with value, summary, and format)
$result = $this->getDatabase()->query(‘
SELECT
fld.body_value,
fld.body_summary,
fld.body_format
FROM
{field_data_body} fld
WHERE
fld.entity_id = :nid
‘, array(‘:nid’ => $nid));
foreach ($result as $record) {
$row->setSourceProperty(‘body_value’, $record->body_value );
$row->setSourceProperty(‘body_summary’, $record->body_summary );
$row->setSourceProperty(‘body_format’, $record->body_format );
}

// taxonomy term IDs
// (here we use MySQL’s GROUP_CONCAT() function to merge all values into one row.)
$result = $this->getDatabase()->query(‘
SELECT
GROUP_CONCAT(fld.field_tags_tid) as tids
FROM
{field_data_field_tags} fld
WHERE
fld.entity_id = :nid
‘, array(‘:nid’ => $nid));
foreach ($result as $record) {
if (!is_null($record->tids)) {
$row->setSourceProperty(‘tags’, explode(‘,’, $record->tids) );
}
}

// images
$result = $this->getDatabase()->query(‘
SELECT
fld.field_image_fid,
fld.field_image_alt,
fld.field_image_title,
fld.field_image_width,
fld.field_image_height
FROM
{field_data_field_image} fld
WHERE
fld.entity_id = :nid
‘, array(‘:nid’ => $nid));
// Create an associative array for each row in the result. The keys
// here match the last part of the column name in the field table.
$images = [];
foreach ($result as $record) {
$images[] = [
‘target_id’ => $record->field_files_fid,
‘alt’ => $record->field_image_alt,
‘title’ => $record->field_image_title,
‘width’ => $record->field_image_width,
‘height’ => $record->field_image_height,
];
}
$row->setSourceProperty(‘images’, $images);

return parent::prepareRow($row);
}

/**
* {@inheritdoc}
*/
public function getIds() {
$ids[‘nid’][‘type’] = ‘integer’;
$ids[‘nid’][‘alias’] = ‘n’;
return $ids;
}

/**
* {@inheritdoc}
*/
public function bundleMigrationRequired() {
return FALSE;
}

/**
* {@inheritdoc}
*/
public function entityTypeId() {
return ‘node’;
}

/**
* Returns the user base fields to be migrated.
*
* @return array
* Associative array having field name as key and description as value.
*/
protected function baseFields() {
$fields = array(
‘nid’ => $this->t(‘Node ID’),
‘vid’ => $this->t(‘Version ID’),
‘type’ => $this->t(‘Type’),
‘title’ => $this->t(‘Title’),
‘format’ => $this->t(‘Format’),
‘teaser’ => $this->t(‘Teaser’),
‘uid’ => $this->t(‘Authored by (uid)’),
‘created’ => $this->t(‘Created timestamp’),
‘changed’ => $this->t(‘Modified timestamp’),
‘status’ => $this->t(‘Published’),
‘promote’ => $this->t(‘Promoted to front page’),
‘sticky’ => $this->t(‘Sticky at top of lists’),
‘language’ => $this->t(‘Language (fr, en, …)’),
);
return $fields;
}

}

 

click here 4501
click here 4502
click here 4503
click here 4504
click here 4505
click here 4506
click here 4507
click here 4508
click here 4509
click here 4510
click here 4511
click here 4512
click here 4513
click here 4514
click here 4515
click here 4516
click here 4517
click here 4518
click here 4519
click here 4520
click here 4521
click here 4522
click here 4523
click here 4524
click here 4525
click here 4526
click here 4527
click here 4528
click here 4529
click here 4530
click here 4531
click here 4532
click here 4533
click here 4534
click here 4535
click here 4536
click here 4537
click here 4538
click here 4539
click here 4540
click here 4541
click here 4542
click here 4543
click here 4544
click here 4545
click here 4546
click here 4547
click here 4548
click here 4549
click here 4550
click here 4551
click here 4552
click here 4553
click here 4554
click here 4555
click here 4556
click here 4557
click here 4558
click here 4559
click here 4560
click here 4561
click here 4562
click here 4563
click here 4564
click here 4565
click here 4566
click here 4567
click here 4568
click here 4569
click here 4570
click here 4571
click here 4572
click here 4573
click here 4574
click here 4575
click here 4576
click here 4577
click here 4578
click here 4579
click here 4580
click here 4581
click here 4582
click here 4583
click here 4584
click here 4585
click here 4586
click here 4587
click here 4588
click here 4589
click here 4590
click here 4591
click here 4592
click here 4593
click here 4594
click here 4595
click here 4596
click here 4597
click here 4598
click here 4599
click here 4600
click here 4601
click here 4602
click here 4603
click here 4604
click here 4605
click here 4606
click here 4607
click here 4608
click here 4609
click here 4610
click here 4611
click here 4612
click here 4613
click here 4614
click here 4615
click here 4616
click here 4617
click here 4618
click here 4619
click here 4620
click here 4621
click here 4622
click here 4623
click here 4624
click here 4625
click here 4626
click here 4627
click here 4628
click here 4629
click here 4630
click here 4631
click here 4632
click here 4633
click here 4634
click here 4635
click here 4636
click here 4637
click here 4638
click here 4639
click here 4640
click here 4641
click here 4642
click here 4643
click here 4644
click here 4645
click here 4646
click here 4647
click here 4648
click here 4649
click here 4650
click here 4651
click here 4652
click here 4653
click here 4654
click here 4655
click here 4656
click here 4657
click here 4658
click here 4659
click here 4660
click here 4661
click here 4662
click here 4663
click here 4664
click here 4665
click here 4666
click here 4667
click here 4668
click here 4669
click here 4670
click here 4671
click here 4672
click here 4673
click here 4674
click here 4675
click here 4676
click here 4677
click here 4678
click here 4679
click here 4680
click here 4681
click here 4682
click here 4683
click here 4684
click here 4685
click here 4686
click here 4687
click here 4688
click here 4689
click here 4690
click here 4691
click here 4692
click here 4693
click here 4694
click here 4695
click here 4696
click here 4697
click here 4698
click here 4699
click here 4700
click here 4701
click here 4702
click here 4703
click here 4704
click here 4705
click here 4706
click here 4707
click here 4708
click here 4709
click here 4710
click here 4711
click here 4712
click here 4713
click here 4714
click here 4715
click here 4716
click here 4717
click here 4718
click here 4719
click here 4720
click here 4721
click here 4722
click here 4723
click here 4724
click here 4725
click here 4726
click here 4727
click here 4728
click here 4729
click here 4730
click here 4731
click here 4732
click here 4733
click here 4734
click here 4735
click here 4736
click here 4737
click here 4738
click here 4739
click here 4740
click here 4741
click here 4742
click here 4743
click here 4744
click here 4745
click here 4746
click here 4747
click here 4748
click here 4749
click here 4750
click here 4751
click here 4752
click here 4753
click here 4754
click here 4755
click here 4756
click here 4757
click here 4758
click here 4759
click here 4760
click here 4761
click here 4762
click here 4763
click here 4764
click here 4765
click here 4766
click here 4767
click here 4768
click here 4769
click here 4770
click here 4771
click here 4772
click here 4773
click here 4774
click here 4775
click here 4776
click here 4777
click here 4778
click here 4779
click here 4780
click here 4781
click here 4782
click here 4783
click here 4784
click here 4785
click here 4786
click here 4787
click here 4788
click here 4789
click here 4790
click here 4791
click here 4792
click here 4793
click here 4794
click here 4795
click here 4796
click here 4797
click here 4798
click here 4799
click here 4800
click here 4801
click here 4802
click here 4803
click here 4804
click here 4805
click here 4806
click here 4807
click here 4808
click here 4809
click here 4810
click here 4811
click here 4812
click here 4813
click here 4814
click here 4815
click here 4816
click here 4817
click here 4818
click here 4819
click here 4820
click here 4821
click here 4822
click here 4823
click here 4824
click here 4825
click here 4826
click here 4827
click here 4828
click here 4829
click here 4830
click here 4831
click here 4832
click here 4833
click here 4834
click here 4835
click here 4836
click here 4837
click here 4838
click here 4839
click here 4840
click here 4841
click here 4842
click here 4843
click here 4844
click here 4845
click here 4846
click here 4847
click here 4848
click here 4849
click here 4850
click here 4851
click here 4852
click here 4853
click here 4854
click here 4855
click here 4856
click here 4857
click here 4858
click here 4859
click here 4860
click here 4861
click here 4862
click here 4863
click here 4864
click here 4865
click here 4866
click here 4867
click here 4868
click here 4869
click here 4870

Related Articles

Leave a Reply

Back to top button