You are here

public function Importer::loadTarget in Feeds Paragraphs 8

Loads the existing paragraphs from db.

Parameters

EntityInterface $entity: The host entity.

FieldConfigInterface $targetInstance: The target field instance. The entity storage.

array $result: The previously loaded paragraphs to append to.

Return value

Paragraph[] The loaded Paragraphs entities.

1 call to Importer::loadTarget()
Importer::initHostParagraphs in src/Importer.php
Creates empty host Paragraphs entities or gets the existing ones.

File

src/Importer.php, line 298

Class

Importer

Namespace

Drupal\feeds_para_mapper

Code

public function loadTarget($entity, $targetInstance, array $result = array()) {
  $targetInfo = $targetInstance
    ->get('target_info');
  $path = $targetInfo->path;
  $target = $targetInstance
    ->getName();
  if (count($path) > 1) {
    $path[] = array(
      'host_field' => $target,
    );
    if ($exist = $entity
      ->hasField($target)) {
      $result[] = $entity;
    }
    else {
      foreach ($path as $host_info) {
        if ($exist = $entity
          ->hasField($host_info['host_field'])) {
          $values = $entity
            ->get($host_info['host_field'])
            ->getValue();
          foreach ($values as $value) {
            $paragraph = $this->paragraph_storage
              ->load($value['target_id']);
            if ($paragraph) {
              $result = $this
                ->loadTarget($paragraph, $targetInstance, $result);
            }
          }
        }
      }
    }
  }
  elseif ($entity
    ->hasField($path[0]['host_field'])) {
    $values = $entity
      ->get($path[0]['host_field'])
      ->getValue();
    foreach ($values as $value) {
      $paragraph = $this->paragraph_storage
        ->load($value['target_id']);
      if ($paragraph) {
        $result[] = $paragraph;
      }
    }
  }
  return $result;
}