You are here

protected function Importer::preAddToImport in Default Content Deploy 8

Here we can edit data`s value before importing.

Parameters

$data:

Return value

$this

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 call to Importer::preAddToImport()
Importer::decodeFile in src/Importer.php
Prepare file to import.

File

src/Importer.php, line 409

Class

Importer
A service for handling import of default content.

Namespace

Drupal\default_content_deploy

Code

protected function preAddToImport(&$data) {
  $decode = $data['data'];
  $uuid = $decode['uuid'][0]['value'];
  $entity_type_id = $data['entity_type_id'];

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this->entityRepository
    ->loadEntityByUuid($entity_type_id, $uuid);
  $entity_type_object = $this->entityTypeManager
    ->getDefinition($entity_type_id);

  // Keys of entity.
  $key_id = $entity_type_object
    ->getKey('id');
  $key_revision_id = $entity_type_object
    ->getKey('revision');

  // Some old exports don't have the entity ID.
  if (isset($decode[$key_id][0]['value'])) {
    $this->entityLookup[$entity_type_id][$decode[$key_id][0]['value']] = $uuid;
  }
  if ($entity) {
    $is_new = FALSE;
    $status = 'update';

    // Replace entity ID.
    $decode[$key_id][0]['value'] = $entity
      ->id();

    // Skip if the Changed time the same or less in the file.
    if ($entity instanceof EntityChangedInterface) {
      $changed_time_file = 0;
      foreach ($decode['changed'] as $changed) {
        $changed_time = strtotime($changed['value']);
        if ($changed_time > $changed_time_file) {
          $changed_time_file = $changed_time;
        }
      }
      if (!$this->forceOverride && $changed_time_file <= $entity
        ->getChangedTimeAcrossTranslations()) {
        $status = 'skip';
      }
    }
    elseif (!$this->forceOverride) {
      $this->linkManager
        ->setLinkDomain($this
        ->getLinkDomain($data));
      $current_entity_decoded = $this->serializer
        ->decode($this->exporter
        ->getSerializedContent($entity), 'hal_json');
      $diff = ArrayDiffMultidimensional::looseComparison($decode, $current_entity_decoded);
      if (!$diff) {
        $status = 'skip';
      }

      // @todo is this still needed?
      $this->linkManager
        ->setLinkDomain(FALSE);
    }
  }
  else {
    $status = 'create';
    $is_new = TRUE;

    // Ignore ID for creating a new entity.
    unset($decode[$key_id]);
  }

  // @see path_entity_base_field_info().
  // @todo offer an event to let third party modules register their content
  //       types.
  if (in_array($entity_type_id, [
    'taxonomy_term',
    'node',
    'media',
  ])) {
    unset($decode['path']);
  }

  // Ignore revision and id of entity.
  unset($decode[$key_revision_id]);
  $data['is_new'] = $is_new;
  $data['status'] = $status;
  $data['data'] = $decode;
  $data['key_id'] = $key_id;
  return $this;
}