You are here

private function Importer::checkValuesChanges in Feeds Paragraphs 8

Determines whether the values are new, updated, or should be removed.

Parameters

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

array $entities: The existing Paragraphs entities.

Return value

array Information about each value state.

2 calls to Importer::checkValuesChanges()
Importer::appendParagraphs in src/Importer.php
Creates and updates new paragraphs entities when needed.
Importer::updateParagraphs in src/Importer.php
Removes unwanted Paragraphs entities, and marks others for values changes.

File

src/Importer.php, line 661

Class

Importer

Namespace

Drupal\feeds_para_mapper

Code

private function checkValuesChanges(array $slices, array $entities) {
  $target = $this->target
    ->getName();
  $lang = $this->language;
  $getParagraph = function ($index) use ($entities) {
    if (isset($entities[$index])) {
      return $entities[$index];
    }
    return NULL;
  };
  $getValuesState = function ($paragraph, $chunk) use ($target, $lang) {
    $state = "new";
    if (!isset($paragraph)) {
      return $state;
    }
    if (!isset($paragraph->{$target})) {
      return "shareable";
    }
    $foundValues = array();
    $targetValues = $paragraph
      ->get($target)
      ->getValue();
    foreach ($chunk as $index => $chunkVal) {
      $found_sub_fields = array();
      $changed = NULL;
      if (isset($targetValues[$index])) {
        $targetValue = $targetValues[$index];
        foreach ($chunkVal as $sub_field => $sub_field_value) {
          if (isset($targetValue[$sub_field])) {
            $found_sub_fields[] = $sub_field;
            $changed = $targetValue[$sub_field] !== $sub_field_value;
          }
          else {
            $changed = TRUE;
            break;
          }
        }
      }
      if (count($found_sub_fields) <= count(array_keys($chunkVal))) {
        $value = [
          "chunk_value" => $chunkVal,
          'state' => $changed ? "changed" : "unchanged",
        ];
        $foundValues[] = $value;
      }
    }
    $changed = FALSE;
    foreach ($foundValues as $foundValue) {
      if ($foundValue['state'] === "changed") {
        $changed = TRUE;
        break;
      }
    }
    if (!count($targetValues)) {
      $changed = TRUE;
    }
    $state = $changed ? "changed" : "unchanged";
    return $state;
  };
  for ($i = 0; $i < count($slices); $i++) {
    $par = $getParagraph($i);
    $state = $getValuesState($par, $slices[$i]);
    $slices[$i]['state'] = $state;
  }

  // Search for empty paragraphs:
  for ($i = 0; $i < count($entities); $i++) {
    $has_common = FALSE;
    $in_common = $this->mapper
      ->getInfo($this->target, 'in_common');
    if (isset($in_common)) {
      $has_common = TRUE;
      $empty_commons = array();
      foreach ($in_common as $fieldInfo) {
        if (!isset($entities[$i]->{$field['name']})) {
          $empty_commons[] = $fieldInfo;
        }
      }

      // If all other fields are empty, we should delete this entity:
      if (count($empty_commons) === count($in_common)) {
        $has_common = FALSE;
      }
    }
    if (!isset($slices[$i]) && !$has_common) {
      $slices[$i] = array(
        'state' => 'remove',
      );
    }
  }
  return $slices;
}