public function JsonapiHelper::importEntityListData in Entity Share 8.2
Same name and namespace in other branches
- 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::importEntityListData()
Use data from the JSON:API to import content.
Parameters
array $entity_list_data: An array of data from a JSON:API endpoint.
Return value
int[] The list of entity ids imported.
Overrides JsonapiHelperInterface::importEntityListData
1 call to JsonapiHelper::importEntityListData()
- JsonapiHelper::updateRelationships in modules/
entity_share_client/ src/ Service/ JsonapiHelper.php - Create or update the entity reference field values of an entity.
File
- modules/
entity_share_client/ src/ Service/ JsonapiHelper.php, line 420
Class
- JsonapiHelper
- Class JsonapiHelper.
Namespace
Drupal\entity_share_client\ServiceCode
public function importEntityListData(array $entity_list_data) {
// Allow other modules to alter the entity data with an EventSubscriber.
$event = new EntityListDataAlterEvent($entity_list_data, $this->remote);
$this->eventDispatcher
->dispatch(EntityListDataAlterEvent::EVENT_NAME, $event);
$entity_list_data = $event
->getEntityListData();
$imported_entity_ids = [];
foreach (EntityShareUtility::prepareData($entity_list_data) as $entity_data) {
$parsed_type = explode('--', $entity_data['type']);
$entity_type_id = $parsed_type[0];
$entity_bundle = $parsed_type[1];
$resource_type = $this->resourceTypeRepository
->get($entity_type_id, $entity_bundle);
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity_keys = $entity_storage
->getEntityType()
->getKeys();
$this
->prepareEntityData($entity_data, $entity_keys);
$data_langcode = !empty($entity_keys['langcode']) ? $entity_data['attributes'][$resource_type
->getPublicName($entity_keys['langcode'])] : LanguageInterface::LANGCODE_NOT_SPECIFIED;
// Prepare entity label.
if (isset($entity_keys['label'])) {
$entity_label = $entity_data['attributes'][$resource_type
->getPublicName($entity_keys['label'])];
}
else {
// Use the entity type if there is no label.
$entity_label = $entity_type_id;
}
if ($data_langcode && !$this
->dataLanguageExists($data_langcode, $entity_label)) {
continue;
}
// Check if an entity already exists.
// JSON:API no longer includes uuid in attributes so we're using id
// instead. See https://www.drupal.org/node/2984247.
$existing_entities = $entity_storage
->loadByProperties([
'uuid' => $entity_data['id'],
]);
// Here is the supposition that we are importing a list of content
// entities. Currently this is ensured by the fact that it is not possible
// to make a channel on config entities and on users. And that in the
// relationshipHandleable() method we prevent handling config entities and
// users relationships.
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this
->extractEntity($entity_data);
// New entity.
if (empty($existing_entities)) {
// Allow other modules to alter the entity with an EventSubscriber.
$event = new EntityInsertEvent($entity, $this->remote);
$this->eventDispatcher
->dispatch(EntityInsertEvent::EVENT_NAME, $event);
$entity
->save();
$imported_entity_ids[] = $entity
->id();
// Prevent the entity of being reimported.
$this->importedEntities[$entity
->language()
->getId()][$entity
->uuid()] = $entity
->uuid();
$this
->updateRelationships($entity, $entity_data);
$this
->handlePhysicalFiles($entity, $entity_data);
$this
->setChangedTime($entity, $resource_type, $entity_data);
$entity
->save();
}
else {
/** @var \Drupal\Core\Entity\ContentEntityInterface $existing_entity */
$existing_entity = array_shift($existing_entities);
$imported_entity_ids[] = $existing_entity
->id();
if (!isset($this->importedEntities[$data_langcode][$existing_entity
->uuid()])) {
// Prevent the entity translation of being reimported.
$this->importedEntities[$data_langcode][$existing_entity
->uuid()] = $existing_entity
->uuid();
$has_translation = $existing_entity
->hasTranslation($data_langcode);
// Update the existing translation.
if ($has_translation) {
$resource_type = $this->resourceTypeRepository
->get($entity
->getEntityTypeId(), $entity
->bundle());
$existing_translation = $existing_entity
->getTranslation($data_langcode);
foreach ($entity_data['attributes'] as $field_name => $value) {
$field_name = $resource_type
->getInternalName($field_name);
$existing_translation
->set($field_name, $entity
->get($field_name)
->getValue());
}
// Allow other modules to alter the entity with an EventSubscriber.
$event = new EntityAlterEvent($existing_translation, $this->remote);
$this->eventDispatcher
->dispatch(EntityAlterEvent::EVENT_NAME, $event);
$existing_translation
->save();
}
else {
$translation = $entity
->toArray();
$existing_entity
->addTranslation($data_langcode, $translation);
// Allow other modules to alter the entity translation with an
// EventSubscriber.
$event = new EntityAlterEvent($existing_entity
->getTranslation($data_langcode), $this->remote);
$this->eventDispatcher
->dispatch(EntityAlterEvent::EVENT_NAME, $event);
$existing_entity
->save();
$existing_translation = $existing_entity
->getTranslation($data_langcode);
}
$this
->updateRelationships($existing_translation, $entity_data);
$this
->handlePhysicalFiles($existing_translation, $entity_data);
$this
->setChangedTime($existing_translation, $resource_type, $entity_data);
$existing_translation
->save();
}
}
}
return $imported_entity_ids;
}