You are here

public function DefaultDataProcessor::prepareImportableEntityData in Entity Share 8.3

Method called on STAGE_PREPARE_IMPORTABLE_ENTITY_DATA.

If the plugin reacts to this stage.

Parameters

\Drupal\entity_share_client\RuntimeImportContext $runtime_import_context: The import context.

array $entity_json_data: The entity JSON data.

Overrides ImportProcessorPluginBase::prepareImportableEntityData

File

modules/entity_share_client/src/Plugin/EntityShareClient/Processor/DefaultDataProcessor.php, line 127

Class

DefaultDataProcessor
General default data processor.

Namespace

Drupal\entity_share_client\Plugin\EntityShareClient\Processor

Code

public function prepareImportableEntityData(RuntimeImportContext $runtime_import_context, array &$entity_json_data) {
  $field_mappings = $runtime_import_context
    ->getFieldMappings();
  $parsed_type = explode('--', $entity_json_data['type']);
  $entity_type_id = $parsed_type[0];
  $entity_bundle = $parsed_type[1];

  // @todo Refactor in attributes to avoid getting entity keys each time.
  $entity_storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $entity_keys = $entity_storage
    ->getEntityType()
    ->getKeys();
  $entity_keys_to_remove = [
    'id',
    'revision',
    // Remove the default_langcode boolean to be able to import content not
    // necessarily in the default language.
    'default_langcode',
  ];
  foreach ($entity_keys_to_remove as $entity_key_to_remove) {
    if (!isset($entity_keys[$entity_key_to_remove])) {
      continue;
    }

    // If there is nothing in the field mapping, the field should have been
    // disabled on the server website using JSON:API extras.
    if (!isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys[$entity_key_to_remove]])) {
      continue;
    }
    $public_name_to_remove = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys[$entity_key_to_remove]];
    if (isset($entity_json_data['attributes'][$public_name_to_remove])) {
      unset($entity_json_data['attributes'][$public_name_to_remove]);
    }
  }

  // UUID is no longer included as attribute.
  $uuid_public_name = 'uuid';
  if (!empty($entity_keys['uuid']) && isset($field_mappings[$entity_type_id][$entity_bundle][$entity_keys['uuid']])) {
    $uuid_public_name = $field_mappings[$entity_type_id][$entity_bundle][$entity_keys['uuid']];
  }
  $entity_json_data['attributes'][$uuid_public_name] = $entity_json_data['id'];
}