public function JsonapiHelper::importEntityListData in Entity Share 8
Same name and namespace in other branches
- 8.2 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::importEntityListData()
Use data from the JSONAPI to import content.
Parameters
array $entity_list_data: An array of data from a JSONAPI 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 317
Class
- JsonapiHelper
- Class JsonapiHelper.
Namespace
Drupal\entity_share_client\ServiceCode
public function importEntityListData(array $entity_list_data) {
$imported_entity_ids = [];
foreach ($this
->prepareData($entity_list_data) as $entity_data) {
$parsed_type = explode('--', $entity_data['type']);
$entity_type = $this->entityDefinitionUpdateManager
->getEntityType($parsed_type[0]);
$entity_keys = $entity_type
->getKeys();
$this
->prepareEntityData($entity_data, $entity_keys);
$data_langcode = $entity_data['attributes'][$entity_keys['langcode']];
// Prepare entity label.
if (isset($entity_keys['label'])) {
$entity_label = $entity_data['attributes'][$entity_keys['label']];
}
else {
// Use the entity type if there is no label.
$entity_label = $parsed_type[0];
}
if (!$this
->dataLanguageExists($data_langcode, $entity_label)) {
continue;
}
// Check if an entity already exists.
$existing_entities = $this->entityTypeManager
->getStorage($parsed_type[0])
->loadByProperties([
'uuid' => $entity_data['attributes'][$entity_keys['uuid']],
]);
// 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)) {
$entity
->save();
$imported_entity_ids[] = $entity
->id();
// Prevent the entity of being reimported.
$this->importedEntities[] = $entity
->uuid();
$this
->updateRelationships($entity, $entity_data);
$this
->handlePhysicalFiles($entity, $entity_data);
// Change the entity "changed" time because it could have been altered
// with relationship save by example.
if (method_exists($entity, 'setChangedTime')) {
$entity
->setChangedTime($entity_data['attributes']['changed']);
}
$entity
->save();
}
else {
/** @var \Drupal\Core\Entity\ContentEntityInterface $existing_entity */
$existing_entity = array_shift($existing_entities);
$imported_entity_ids[] = $existing_entity
->id();
if (!in_array($existing_entity
->uuid(), $this->importedEntities)) {
// Prevent the entity of being reimported.
$this->importedEntities[] = $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());
}
$existing_translation
->save();
}
else {
$translation = $entity
->toArray();
$existing_entity
->addTranslation($data_langcode, $translation);
$existing_entity
->save();
$existing_translation = $existing_entity
->getTranslation($data_langcode);
}
$this
->updateRelationships($existing_translation, $entity_data);
$this
->handlePhysicalFiles($existing_translation, $entity_data);
// Change the entity "changed" time because it could have been altered
// with relationship save by example.
if (method_exists($existing_translation, 'setChangedTime')) {
$existing_translation
->setChangedTime($entity_data['attributes']['changed']);
}
$existing_translation
->save();
}
}
}
return $imported_entity_ids;
}