forked from backdrop-contrib/paragraphs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparagraphs.node_clone.inc
89 lines (79 loc) · 3.12 KB
/
paragraphs.node_clone.inc
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
86
87
88
89
<?php
/**
* @file
* Holds relevant functions for paragraph's node clone integration.
*/
/**
* Implements hook_clone_node_alter().
*/
function paragraphs_clone_node_alter(&$node, $context) {
foreach (field_info_fields() as $field_name => $field) {
if ($field['type'] == 'paragraphs' && isset($node->$field_name)) {
foreach ($node->$field_name as $field_language => $values) {
paragraphs_clone_items('node', $node, $field_name, $field_language);
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function paragraphs_form_node_form_alter(&$form, &$form_state, $form_id) {
// Alter the node edit forms for cloned nodes.
if (('clone' == arg(2)) && ('_node_form' == substr($form_id, -10))) {
// Go through all fields.
foreach ($form_state['field'] as &$field_config) {
$langcode = key($field_config);
// Only find fields containing paragraphs items.
if (isset($field_config[$langcode]['field']['type']) && ($field_config[$langcode]['field']['type'] == 'paragraphs')) {
// Unset the item_id and revision_id of each paragraphs item so
// that new items are created on save.
foreach ($field_config as $langcode => $items) {
if (isset($items['entity']) && count($items['entity'])) {
foreach ($items['entity'] as $paragraph_item) {
$paragraph_item->item_id = NULL;
$paragraph_item->revision_id = NULL;
}
}
}
}
}
}
}
/**
* Clone a Paragraphs item. Helper function for hook_clone_node_alter().
*/
function paragraphs_clone_items($entity_type, &$entity, $field_name, $langcode = LANGUAGE_NONE) {
if (empty($entity->{$field_name}[$langcode])) {
return;
}
// Resets the field_language static. Otherwise, the language returned by
// field_language() is FALSE, resulting in metadata-wrappers (and
// field_get_items()) returning FALSE or an empty array, which means that the
// fields in it won't be cloned.
backdrop_static_reset('field_language');
$old_items = $entity->{$field_name}[$langcode];
if (!is_array($old_items)) {
$old_items = array($old_items);
}
$old_items = array_map(function ($item) {
return paragraphs_item_load($item['value']);
}, $old_items);
unset($entity->{$field_name}[$langcode]);
foreach ($old_items as $old_item) {
list(, , $bundle) = entity_extract_ids('paragraphs_item', $old_item);
/* @var $new_item ParagraphsItemEntity */
$new_item = entity_create('paragraphs_item', array('bundle' => $bundle, 'field_name' => $field_name));
$new_item->setHostEntity($entity_type, $entity, $langcode);
// Check if any of the fields in the newly cloned fc item is a paragraph.
foreach (field_info_instances('paragraphs_item', $bundle) as $new_field_name => $new_field_instance) {
if (!empty($old_item->{$new_field_name})) {
$new_item->{$new_field_name} = $old_item->{$new_field_name};
$field_info = field_info_field($new_field_name);
if ($field_info['type'] == 'paragraphs') {
paragraphs_clone_items('paragraphs_item', $new_item, $new_field_name, $langcode);
}
}
}
}
}