function DevelDumperManager::entityToArrayWithReferences
Converts the given entity to an array with referenced entities loaded.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The target entity.
int $depth: Internal. Track the recursion.
array $array_path: Internal. Track where we first say this entity.
Return value
mixed[] An array of field names and deep values.
2 calls to DevelDumperManager::entityToArrayWithReferences()
- DevelDumperManager::export in src/
DevelDumperManager.php - Returns a string representation of a variable.
- DevelDumperManager::exportAsRenderable in src/
DevelDumperManager.php - Returns a render array representation of a variable.
File
-
src/
DevelDumperManager.php, line 205
Class
- DevelDumperManager
- Manager class for DevelDumper.
Namespace
Drupal\develCode
protected function entityToArrayWithReferences(EntityInterface $entity, int $depth = 0, array $array_path = []) {
// Note that we've now seen this entity.
$seen =& drupal_static(__FUNCTION__);
$seen_key = $entity->getEntityTypeId() . '-' . $entity->id();
if (!isset($seen[$seen_key])) {
$seen[$seen_key] = $array_path;
}
$array = $entity->toArray();
// Prevent out of memory and too deep traversing.
if ($depth > 20) {
return $array;
}
if (!$entity instanceof FieldableEntityInterface) {
return $array;
}
foreach ($array as $field => &$value) {
if (is_array($value)) {
$fieldDefinition = $entity->getFieldDefinition($field);
$target_type = $fieldDefinition->getSetting('target_type');
if (!$target_type) {
continue;
}
try {
$storage = $this->entityTypeManager
->getStorage($target_type);
} catch (InvalidPluginDefinitionException|PluginNotFoundException) {
continue;
}
foreach ($value as $delta => &$item) {
if (is_array($item)) {
$referenced_entity = NULL;
if (isset($item['target_id'])) {
$referenced_entity = $storage->load($item['target_id']);
}
elseif (isset($item['target_revision_id'])) {
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
$referenced_entity = $storage->loadRevision($item['target_revision_id']);
}
$langcode = $entity->language()
->getId();
if ($referenced_entity instanceof TranslatableInterface && $referenced_entity->hasTranslation($langcode)) {
$referenced_entity = $referenced_entity->getTranslation($langcode);
}
if (empty($referenced_entity)) {
continue;
}
$seen_id = $referenced_entity->getEntityTypeId() . '-' . $referenced_entity->id();
if (isset($seen[$seen_id])) {
$item['message'] = 'Recursion detected.';
$item['array_path'] = implode('.', $seen[$seen_id]);
continue;
}
$item['entity'] = $this->entityToArrayWithReferences($referenced_entity, $depth++, array_merge($array_path, [
$field,
$delta,
'entity',
]));
$item['bundle'] = $referenced_entity->bundle();
}
}
}
}
return $array;
}