This repository was archived by the owner on Nov 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparagraph_split.module
More file actions
46 lines (38 loc) · 1.71 KB
/
paragraph_split.module
File metadata and controls
46 lines (38 loc) · 1.71 KB
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
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\user\Entity\User;
use Drupal\node\Entity\Node;
/**
* Implements hook_ENTITY_TYPE_insert() for 'nodes'
* When a node is created, with content in the temporary field, create the associated paragraph instead
*/
function paragraph_split_node_insert(EntityInterface $node) {
//If node content type is node_type
if ($node->getType() == 'node_type') {
if (!$node->get('field_title')->isEmpty) {
$new_paragraphs = array();
$count = count($node->get('field_title'));
for ($k = 0; $k < $count; $k++) {
//This is needed to handle when there is a title without a matching description
$desc = (empty($node->get('field_description')[$k])) ? '' : $node->get('field_description')[$k]->getString();
//Create new paragraph entity
$paragraph = Paragraph::create([
'type' => 'paragraph_type',
'field_title' => $node->get('field_title')[$k],
'field_description' => $desc,
'uid' => 1,
]);
$paragraph->save();
//Add the new paragraph to the array
array_push($new_paragraphs,array(
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
));
}
//Overrides if there was already one in place, which there shouldn't be in this context of an import
$node->field_paragraph_reference = $new_paragraphs;
}
$node->save();
}
}