You are here

function paragraphs_clone_items in Paragraphs 7

Clone a Paragraphs item. Helper function for hook_clone_node_alter().

1 call to paragraphs_clone_items()
paragraphs_clone_node_alter in ./paragraphs.node_clone.inc
Implements hook_clone_node_alter().

File

./paragraphs.node_clone.inc, line 50
Holds relevant functions for paragraph's node clone integration.

Code

function paragraphs_clone_items($entity_type, &$entity, $field_name, $language = LANGUAGE_NONE) {
  if (empty($entity->{$field_name}[$language])) {
    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.
  drupal_static_reset('field_language');
  $old_items = $entity->{$field_name}[$language];
  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}[$language]);
  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, $language);

    // 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, $language);
        }
      }
    }
  }
}