You are here

private function Importer::shouldCreateNew in Feeds Paragraphs 8

Checks whether we should create new Paragraphs.

When we find existing attached paragraphs entities while updating, we use this to determine if we can create new paragraph entities.

Parameters

EntityInterface $entity: The currently attached Paragraphs entity.

array $slices: The sliced values based on user choice & the field cardinality.

array $futureValue: The value that the target field will hold.

Return value

bool TRUE if we should create new Paragraphs entity.

2 calls to Importer::shouldCreateNew()
Importer::createParagraphs in src/Importer.php
Creates new Paragraphs entities, and marks others for values changes.
Importer::initHostParagraphs in src/Importer.php
Creates empty host Paragraphs entities or gets the existing ones.

File

src/Importer.php, line 605

Class

Importer

Namespace

Drupal\feeds_para_mapper

Code

private function shouldCreateNew($entity, array $slices, array $futureValue = array()) {
  $path = $this->targetInfo->path;
  if (count($path) > 1 && $entity instanceof Paragraph) {
    $host_field = $path[count($path) - 1]['host_field'];
    $host = $entity
      ->getParentEntity();
  }
  else {
    $target = $path[0]['host_field'];
    $host_field = $target;
    $host = $this->entity;
  }
  $current_values = $host
    ->get($host_field)
    ->getValue();
  $host_field_storage = $host
    ->get($host_field)
    ->getFieldDefinition()
    ->getFieldStorageDefinition();
  $allowed = (int) $host_field_storage
    ->getCardinality();
  $skip_check = $allowed === -1;

  // If the parent cannot hold anymore values, we should not:
  if ($allowed === count($current_values) && !$skip_check) {
    return FALSE;
  }
  $exceeded = TRUE;
  if ($skip_check) {
    $max = $this->mapper
      ->getMaxValues($this->target, $this->configuration);
    $allowed = $max;

    // Compare the child entity values with max values allowed:
    // Compare the child entity values with max values allowed:
    if (count($futureValue)) {
      if (count($futureValue) < $max) {
        $exceeded = FALSE;
      }
    }
    else {
      $exceeded = FALSE;
    }
  }

  // If the parent or the child entity can hold more values (children),
  // and the child cannot hold values, we should:
  if (count($current_values) < count($slices) && $allowed > count($current_values) && $exceeded) {
    return TRUE;
  }
  if ($skip_check) {
    return TRUE;
  }
  return FALSE;
}