-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomNestedParagraph.php
85 lines (77 loc) · 2.76 KB
/
CustomNestedParagraph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Drupal\migrate_missing_content_translations\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Runs an array of arrays through its own process pipeline.
*
* First, I tried the OOTB sub_process plugin but it couldn't work in this scenario.
*
* @MigrateProcessPlugin(
* id = "custom_nested_paragraph",
* handle_multiples = TRUE
* )
*/
class CustomNestedParagraph extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$return = [];
// The recommendation is to use the OOTB sub_process plugin.
// Since it didn't work in this case so I developed this custom plugin.
// After triaging what expected in the Nested Paragraph field in Article node
// i.e. field_article_nested_items, it was easy to write this plugin.
// The expected array was something like below:
// array:2 [
// 0 => array:2 [
// "target_id" => "32241"
// "target_revision_id" => "3031011"
// ]
// 1 => array:2 [
// "target_id" => "32242"
// "target_revision_id" => "3031012"
// ]
// ]
// Hence, iterates all the paragraph items and get the source &
// destination id. After iterating through all the paragraphs
// return the final nested paragraph array.
$nested_paragraphs_items = [
'migrate_nested_paragraphs_banner_content',
'migrate_nested_paragraphs_hero_content',
];
if (is_array($value)) {
$connection = \Drupal::database();
foreach ($value as $new_value) {
foreach ($nested_paragraphs_items as $paragraphs_item) {
$query = $connection->select('migrate_map_' . $paragraphs_item, 't');
$query->fields('t', ['destid1', 'destid2']);
$query->condition('t.sourceid1', $new_value['value']);
$results = $query->execute()->fetchAll();
foreach ($results as $field_name) {
$return[] = [
'target_id' => $field_name->destid1,
'target_revision_id' => $field_name->destid2
];
}
}
}
}
// dump(
// '---------------------------------------------------------------------',
// '| $custom_nested_paragraph_process_return |',
// '---------------------------------------------------------------------',
// $return,
// '---------------------------------------------------------------------',
// '| $custom_nested_paragraph_process_return |',
// );
return $return;
}
/**
* {@inheritdoc}
*/
public function multiple() {
return TRUE;
}
}