function _content_sync_db_to_entity in Content Synchronization 8
Convert a content entity from the db to an array EXPORT content.
Parameters
$entity_type: Content entity type.
$entity_bundle: Content entity bundle.
array $entity_id: Content entity id.
Return value
array $entity
5 calls to _content_sync_db_to_entity()
- ContentSingleExportForm::updateExport in src/
Form/ ContentSingleExportForm.php - Handles switching the export textarea.
- content_sync_entity_update in ./
content_sync.module - hook_entity_update Keep the content snapshot table synced
- processContentExportFiles in ./
content_sync.batch.inc - Processes the content archive export batch
- processContentSyncSnapshot in ./
content_sync.batch.inc - Processes the content snapshot batch - when the module is installed
- _content_sync_entity_to_db in ./
content_sync.batch.inc - Process a content entity YAML and store to the db IMPORT Content.
File
- ./
content_sync.batch.inc, line 615
Code
function _content_sync_db_to_entity($entity_type, $entity_bundle, $entity_id) {
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
// Get Entity Fields.
$fields = array_filter($entityFieldManager
->getFieldDefinitions($entity_type, $entity_bundle), function ($field_definition) {
return $field_definition;
});
// Initialize array of elements to export.
$entity_elements = [];
foreach ($fields as $fieldID => $field) {
$entity_elements[$field
->getName()] = $field
->getName();
}
// Get Entity Properties - to know the id and bundle fields.
$properties = $entityTypeManager
->getDefinitions()[$entity_type]
->getKeys();
// Get data to fill the yaml.
$entity_data = $entityTypeManager
->getStorage($entity_type)
->load($entity_id);
$entity = [];
$entity['entity_type'] = $entity_type;
$entity['bundle'] = $entity_bundle;
// Remove property ID as we are gonna use UUID to avoid conflicts.
unset($entity_elements[$properties['id']]);
// Remove bundle as it is defined already
unset($entity_elements[$properties['bundle']]);
// Remove vid to avoid conflicts w/versions
unset($entity_elements['vid']);
// Filter array
$entity_elements = array_filter($entity_elements);
//Get entity values
foreach ($entity_elements as $elementID => $element) {
//Include parent UUID if it exist
if ($element == 'parent') {
$parent = $entityTypeManager
->getStorage($entity_type)
->loadParents($entity_id);
$parent = reset($parent);
if (!empty($parent)) {
$entity['values'][0][$element] = $parent
->uuid();
}
}
else {
$entity['values'][0][$element] = $entity_data
->get($element)
->getValue();
}
//Check if it is an entity reference and use UUID instead of target id.
$element_type = $entity_data
->get($element)
->getFieldDefinition()
->getType();
if ($element_type == "entity_reference" || $element_type == "image" || $element_type == "file") {
if ($entity_data
->get($element)->entity) {
$reference_type = $entity_data
->get($element)->entity
->getEntityType()
->id();
//Loop all the values
foreach ($entity_data
->get($element)
->getValue() as $er_key => $er_val) {
$entity['values'][0][$element][$er_key]['target_id'] = $entityTypeManager
->getStorage($reference_type)
->load($er_val['target_id'])
->uuid();
}
}
}
}
// Exception to get the path as it can not be retrieved as regular value.
// Not check for image because gives an error.
if ($entity_type != "file") {
$internal_path = "/" . $entity_data
->toUrl()
->getInternalPath();
// AliasByPath return internal of alias doesn't exist.
$alias = \Drupal::service('path.alias_manager')
->getAliasByPath($internal_path);
// Only pass the value is Alias exist.
if ($internal_path != $alias) {
$entity['values'][0]['path'] = $alias;
}
}
// Include Translations
$lang_default = $entity['values'][0]['langcode'][0]['value'];
foreach ($entity_data
->getTranslationLanguages() as $langcode => $language) {
$c = 0;
if ($entity_data
->hasTranslation($langcode)) {
$entity_data_translation = $entity_data
->getTranslation($langcode);
// Verify that it is not the default langcode.
if ($langcode != $lang_default) {
foreach ($entity_elements as $elementID => $element) {
// Only translatable elements for translations
if ($fields[$elementID]
->isTranslatable() == TRUE) {
$entity['values'][0]['translations'][$c][$element] = $entity_data_translation
->get($element)
->getValue();
//Check if it is an entity reference and use UUID instead of target id.
$element_type = $entity_data_translation
->get($element)
->getFieldDefinition()
->getType();
if ($element_type == "entity_reference" || $element_type == "image" || $element_type == "file") {
if ($entity_data_translation
->get($element)->entity) {
$reference_type = $entity_data_translation
->get($element)->entity
->getEntityType()
->id();
//Loop all the values
foreach ($entity_data_translation
->get($element)
->getValue() as $er_key => $er_val) {
$entity['values'][0]['translations'][$c][$element][$er_key]['target_id'] = $entityTypeManager
->getStorage($reference_type)
->load($er_val['target_id'])
->uuid();
}
}
}
}
}
//$entity['translations'][$c]['path'] = $entity_data_translation->toUrl()->getInternalPath();
//$c++;
}
}
}
return $entity;
}