Skip to content

Commit

Permalink
Issue #1: Nested paragraphs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
laryn authored Feb 3, 2020
1 parent f082cb1 commit 3367e5f
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions replicate_paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,8 @@ function replicate_paragraphs_replicate_entity_paragraphs_item(&$entity) {
* Implements hook_replicate_field_FIELD_TYPE().
*/
function replicate_paragraphs_replicate_field_paragraphs(&$entity, $entity_type, $field_name) {
foreach ($entity->$field_name as $language => $values) {
replicate_paragraphs_clone_items($entity, $entity_type, $field_name, $language);
}
}

/**
* Implements hook_help().
*/
function replicate_paragraphs_help($path, $arg) {
switch ($path) {
case 'admin/help#replicate_paragraphs_item':
// Return a line-break version of the module README.txt.
return check_markup(file_get_contents(dirname(__FILE__) . "/README.txt"));
foreach ($entity->$field_name as $langcode => $values) {
replicate_paragraphs_clone_items($entity, $entity_type, $field_name, $langcode);
}
}

Expand All @@ -40,19 +29,17 @@ function replicate_paragraphs_help($path, $arg) {
* Entity type.
* @param string $field_name
* Name of the field.
* @param string $language
* Language of the field.
* @param string $langcode
* Language code of the field.
*/
function replicate_paragraphs_clone_items(&$entity, $entity_type, $field_name, $language = LANGUAGE_NONE) {
function replicate_paragraphs_clone_items(&$entity, $entity_type, $field_name, $langcode = LANGUAGE_NONE) {
// Reset the field_language static.
// If we don't, the language returned by field_language()
// is FALSE, resulting in de metadata-wrapper (or field_get_items())
// is FALSE, resulting in the metadata-wrapper (or field_get_items())
// to get a FALSE / an empty array,
// which means that the fields in it won't be cloned.
backdrop_static_reset('field_language');

$entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
$old_items = $entity_wrapper->{$field_name}->value();
$old_items = $entity->{$field_name}[$langcode];
if (is_null($old_items)) {
$old_items = array();
}
Expand All @@ -62,12 +49,14 @@ function replicate_paragraphs_clone_items(&$entity, $entity_type, $field_name, $

// Clean the previous entities from the field, so the new
// can be saved instead.
unset($entity->{$field_name}[$language]);
unset($entity->{$field_name}[$langcode]);

foreach ($old_items as $old_item) {
if (!empty($old_item)) {
$new_item = replicate_clone_entity('paragraphs_item', $old_item);
$new_item->setHostEntity($entity_type, $entity, $language);
$old_paragraph_id = $old_item['value'];
$old_entity = entity_load('paragraphs_item', array($old_paragraph_id));
$new_item = replicate_clone_entity('paragraphs_item', $old_entity[$old_paragraph_id]);
$new_item->setHostEntity($entity_type, $entity, $langcode);
// Don't save the new paragraphs,
// it will be saved along with the host entity.
}
Expand Down

0 comments on commit 3367e5f

Please sign in to comment.