You are here

private function Importer::removeExistingParents in Feeds Paragraphs 8

Remove the created parents of the target field from the parents array.

Parameters

array $parents: The parents of the field @see Mapper::buildPath().

Return value

array The non-existing parents array.

1 call to Importer::removeExistingParents()
Importer::createParents in src/Importer.php
Creates a paragraph entity and its parents entities.

File

src/Importer.php, line 511

Class

Importer

Namespace

Drupal\feeds_para_mapper

Code

private function removeExistingParents(array $parents) {
  $field_manager = $this->field_manager;
  $findByField = function ($entity, $field) use (&$findByField, $field_manager) {
    $p_c = Paragraph::class;
    $found = NULL;
    if (get_class($entity) === $p_c) {
      if ($exist = $entity
        ->hasField($field) && count($entity
        ->get($field)
        ->getValue())) {
        $found = $entity;
      }
    }
    else {
      if ($exist = $entity
        ->hasField($field) && count($entity
        ->get($field)
        ->getValue())) {
        $found = $entity;
      }
      else {
        $type = $entity
          ->getEntityTypeId();
        $bundle = $entity
          ->bundle();
        $fields = $field_manager
          ->getFieldDefinitions($type, $bundle);
        $fields = array_filter($fields, function ($field) {
          return $field instanceof FieldConfigInterface;
        });
        foreach ($fields as $field_name => $entity_field) {
          if ($entity_field
            ->getType() === 'entity_reference_revisions') {
            $values = $entity
              ->get($field_name)
              ->getValue();
            foreach ($values as $value) {
              $found = $findByField($value['entity'], $field);
            }
          }
        }
      }
    }
    return $found;
  };
  $removed = [];
  $to_remove = [];
  for ($i = 0; $i < count($parents); $i++) {
    $par = $findByField($this->entity, $parents[$i]['host_field']);
    if ($par) {
      $removed[] = $par;
      $to_remove[] = $parents[$i]['host_field'];
    }
  }
  $parents = array_filter($parents, function ($item) use ($to_remove) {
    return !in_array($item['host_field'], $to_remove);
  });
  usort($parents, function ($a, $b) {
    return $a['order'] < $b['order'] ? -1 : 1;
  });
  return [
    'parents' => $parents,
    'removed' => $removed,
  ];
}