You are here

function feeds_para_mapper_create_parents in Feeds Paragraphs 7

Creates a paragraph entity and its parents.

Parameters

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

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

Return value

\ParagraphsItemEntity The created paragraph entity.

1 call to feeds_para_mapper_create_parents()
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 1031
Allows feeds to import content to Paragraphs' fields.

Code

function feeds_para_mapper_create_parents(array $mapping, $entity) {
  if (isset($GLOBALS['call_count'])) {
    $GLOBALS['call_count']++;
  }
  else {
    $GLOBALS['call_count'] = 1;
  }
  if ($GLOBALS['call_count'] === 2) {
    $stop = null;
  }
  $parents = $mapping['path'];
  $or_count = count($parents);
  $p = feeds_para_mapper_remove_existing_parents($entity, $mapping, $parents);
  $parents = $p['parents'];
  $first = NULL;
  $last = $entity;
  if (count($parents) < $or_count) {
    $last = end($p['removed']);
  }

  // For first bundle fields, determine the real host:
  if (!isset($mapping['host_field'])) {
    $filtered = array_filter($parents, function ($item) use ($mapping) {
      return $item['host_field'] === $mapping['ctype_field'];
    });
    if (count($filtered)) {
      $parents = array();
      $parents[] = $filtered[0];
    }
  }
  if (empty($parents)) {
    $parents[] = end($mapping['path']);
  }
  $parents = array_filter($parents, function ($item) {
    return isset($item['host_entity']);
  });
  foreach ($parents as $parent) {
    $last = feeds_para_mapper_create_paragraph($parent['bundle'], $parent['host_field'], $last, $parent['host_entity']);
    if (!isset($first)) {
      $first = $last;
    }
  }
  return $first;
}