public function ContentImporter::importEntity in Commerce Demo 8
Same name and namespace in other branches
- 8.2 src/ContentImporter.php \Drupal\commerce_demo\ContentImporter::importEntity()
Imports a given entity.
If an entity with the given UUID already exists, it will be updated.
Parameters
string $entity_type_id: The entity type ID.
array $values: The entity values.
Return value
\Drupal\Core\Entity\EntityInterface The created or updated entity.
2 calls to ContentImporter::importEntity()
- ContentImporter::importAll in src/
ContentImporter.php - Imports all content for the given entity type and bundle.
- ContentImporter::processReferences in src/
ContentImporter.php - Processes reference values before importing.
File
- src/
ContentImporter.php, line 118
Class
- ContentImporter
- Defines the content importer.
Namespace
Drupal\commerce_demoCode
public function importEntity($entity_type_id, array $values) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$wanted_keys = [
'bundle',
'langcode',
'uuid',
];
$wanted_keys = array_combine($wanted_keys, $wanted_keys);
$entity_keys = array_intersect_key($entity_type
->getKeys(), $wanted_keys);
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this
->loadEntityByUuid($entity_type_id, $values['uuid']);
if (!$entity) {
// No existing entity found, create a new one.
$initial_values = array_intersect_key($values, array_flip($entity_keys));
$entity = $storage
->create($initial_values);
}
// Process values.
$values = array_diff_key($values, array_flip($entity_keys));
foreach ($entity
->getFieldDefinitions() as $field_name => $definition) {
if (!isset($values[$field_name])) {
continue;
}
$storage_definition = $definition
->getFieldStorageDefinition();
$items = $values[$field_name];
// Re-add the wrapper array stripped by ContentExporter.
if ($storage_definition
->getCardinality() === 1) {
$items = [
$items,
];
}
foreach ($items as $delta => $item) {
if ($definition
->getType() == 'entity_reference' && is_string($item)) {
$target_entity_type_id = $storage_definition
->getSetting('target_type');
$target_entity_type = $this->entityTypeManager
->getDefinition($target_entity_type_id);
if ($target_entity_type
->entityClassImplements(ContentEntityInterface::class)) {
$target_entity = $this
->loadEntityByUuid($target_entity_type_id, $item);
if ($target_entity) {
$items[$delta] = $target_entity
->id();
}
else {
unset($items[$delta]);
}
}
}
elseif ($definition
->getType() == 'image') {
$file = $this
->ensureFile($item['filename']);
$items[$delta] = [
'target_id' => $file
->id(),
] + $item;
}
$values[$field_name] = $items;
}
}
// Perform generic processing.
if (substr($entity_type_id, 0, 9) == 'commerce_') {
$values = $this
->processCommerce($values, $entity);
}
// Process by entity type ID.
if ($entity_type_id == 'commerce_product') {
$values = $this
->processReferences($values, $entity, 'variations');
}
elseif ($entity_type_id == 'commerce_promotion') {
$values = $this
->processReferences($values, $entity, 'coupons');
}
elseif ($entity_type_id == 'taxonomy_term') {
$values = $this
->processTerm($values, $entity);
}
foreach ($values as $field_name => $items) {
$entity
->set($field_name, $items);
}
$entity
->save();
return $entity;
}