You are here

function feeds_para_mapper_duplicate_existing in Feeds Paragraphs 7

Duplicates an existing paragraph entity.

Parameters

array $mapping: Information about the target field and the target paragraph.

object $entity: The entity that is being edited or created.

\ParagraphsItemEntity $existing: The Paragraphs entity to duplicate.

Return value

null|\ParagraphsItemEntity The duplicated entity, or null on failure.

2 calls to feeds_para_mapper_duplicate_existing()
feeds_para_mapper_append_paragraphs in ./feeds_para_mapper.module
Creates and updates new paragraphs entities when needed.
feeds_para_mapper_create_paragraphs in ./feeds_para_mapper.module
Creates new Paragraphs entities, and marks others for values changes.

File

./feeds_para_mapper.module, line 1121
Allows feeds to import content to Paragraphs' fields.

Code

function feeds_para_mapper_duplicate_existing(array $mapping, $entity, \ParagraphsItemEntity $existing) {
  $parents = $mapping['path'];
  $p = feeds_para_mapper_remove_existing_parents($entity, $mapping, $parents);
  $parents = $p['parents'];
  $parents = array_filter($parents, function ($item) {
    return isset($item['host_entity']);
  });
  $lastKey = count($parents) - 1;
  $lastP = isset($parents[$lastKey]) ? $parents[$lastKey] : NULL;
  $parent_field = isset($mapping['host_field']) ? $mapping['host_field'] : $mapping['ctype_field'];
  $host = NULL;
  if (isset($lastP) && $existing->field_name === $lastP['host_field']) {
    $host = $existing
      ->hostEntity();
  }
  elseif ($existing->field_name === $parent_field) {
    if ($parent_field === $mapping['ctype_field']) {
      $host = $entity;
    }
    else {
      $host = $existing
        ->hostEntity();
    }
  }
  if ($host) {

    /*
        When we are inside a create loop,
        the first found entity must have a value
        if not, return it to be filled.
    */
    $target = $mapping['target'];
    $new = $entity->feeds_item->is_new;
    if (!$new && !isset($existing->{$target}) && !isset($existing->to_fill)) {
      $existing->to_fill = TRUE;
      return $existing;
    }

    // Duplicate entity:
    $h_type = $existing
      ->hostEntityType();
    return feeds_para_mapper_create_paragraph($existing
      ->bundle(), $existing->field_name, $host, $h_type);
  }
  else {
    return NULL;
  }
}