public function ContentLoaderBase::buildEntity in YAML Content 8.2
Build an entity from the provided content data.
Parameters
string $entity_type: The machine name for the entity type being created.
array $content_data: Parameters:
- `entity`: (required) The entity type machine name.
- `bundle`: (required) The bundle machine name.
- Additional field and property data keyed by field or property name.
array $context: Contextual data available for more specific entity creation requirements.
Return value
\Drupal\Core\Entity\EntityInterface A built and populated entity object containing the imported data.
Overrides ContentLoaderInterface::buildEntity
1 call to ContentLoaderBase::buildEntity()
- ContentLoaderBase::importEntity in src/
ContentLoader/ ContentLoaderBase.php - Load an entity from a loaded import data outline.
File
- src/
ContentLoader/ ContentLoaderBase.php, line 285
Class
- ContentLoaderBase
- A base ContentLoader implementation to be extended by new content loaders.
Namespace
Drupal\yaml_content\ContentLoaderCode
public function buildEntity($entity_type, array $content_data, array &$context = []) {
// Load entity type definition.
$entity_definition = $this->entityTypeManager
->getDefinition($entity_type);
$entity_keys = $entity_definition
->getKeys();
// Load entity type handler.
$entity_handler = $this->entityTypeManager
->getStorage($entity_type);
// Dispatch the entity import event.
$entity_import_event = new EntityImportEvent($this, $entity_definition, $content_data);
$this->dispatcher
->dispatch(YamlContentEvents::IMPORT_ENTITY, $entity_import_event);
// Map generic entity keys into entity-specific values.
$properties = [];
foreach ($entity_keys as $source => $target) {
if (isset($content_data[$source])) {
$properties[$target] = $content_data[$source];
}
elseif (isset($content_data[$target])) {
$properties[$target] = $content_data[$target];
}
}
// Create the entity.
$entity = $entity_handler
->create($properties);
return $entity;
}