You are here

protected function SqlContentEntityStorage::mapFromStorageRecords in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::mapFromStorageRecords()
  2. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::mapFromStorageRecords()

Maps from storage records to entity objects, and attaches fields.

Parameters

array $records: Associative array of query results, keyed on the entity ID or revision ID.

bool $load_from_revision: (optional) Flag to indicate whether revisions should be loaded or not. Defaults to FALSE.

Return value

array An array of entity objects implementing the EntityInterface.

Overrides EntityStorageBase::mapFromStorageRecords

2 calls to SqlContentEntityStorage::mapFromStorageRecords()
SqlContentEntityStorage::doLoadMultipleRevisionsFieldItems in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Actually loads revision field item values from the storage.
SqlContentEntityStorage::getFromStorage in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Gets entities from the storage.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 469

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function mapFromStorageRecords(array $records, $load_from_revision = FALSE) {
  if (!$records) {
    return [];
  }

  // Get the names of the fields that are stored in the base table and, if
  // applicable, the revision table. Other entity data will be loaded in
  // loadFromSharedTables() and loadFromDedicatedTables().
  $field_names = $this->tableMapping
    ->getFieldNames($this->baseTable);
  if ($this->revisionTable) {
    $field_names = array_unique(array_merge($field_names, $this->tableMapping
      ->getFieldNames($this->revisionTable)));
  }
  $values = [];
  foreach ($records as $id => $record) {
    $values[$id] = [];

    // Skip the item delta and item value levels (if possible) but let the
    // field assign the value as suiting. This avoids unnecessary array
    // hierarchies and saves memory here.
    foreach ($field_names as $field_name) {
      $field_columns = $this->tableMapping
        ->getColumnNames($field_name);

      // Handle field types that store several properties.
      if (count($field_columns) > 1) {
        $definition_columns = $this->fieldStorageDefinitions[$field_name]
          ->getColumns();
        foreach ($field_columns as $property_name => $column_name) {
          if (property_exists($record, $column_name)) {
            $values[$id][$field_name][LanguageInterface::LANGCODE_DEFAULT][$property_name] = !empty($definition_columns[$property_name]['serialize']) ? unserialize($record->{$column_name}) : $record->{$column_name};
            unset($record->{$column_name});
          }
        }
      }
      else {
        $column_name = reset($field_columns);
        if (property_exists($record, $column_name)) {
          $columns = $this->fieldStorageDefinitions[$field_name]
            ->getColumns();
          $column = reset($columns);
          $values[$id][$field_name][LanguageInterface::LANGCODE_DEFAULT] = !empty($column['serialize']) ? unserialize($record->{$column_name}) : $record->{$column_name};
          unset($record->{$column_name});
        }
      }
    }

    // Handle additional record entries that are not provided by an entity
    // field, such as 'isDefaultRevision'.
    foreach ($record as $name => $value) {
      $values[$id][$name][LanguageInterface::LANGCODE_DEFAULT] = $value;
    }
  }

  // Initialize translations array.
  $translations = array_fill_keys(array_keys($values), []);

  // Load values from shared and dedicated tables.
  $this
    ->loadFromSharedTables($values, $translations, $load_from_revision);
  $this
    ->loadFromDedicatedTables($values, $load_from_revision);
  $entities = [];
  foreach ($values as $id => $entity_values) {
    $bundle = $this->bundleKey ? $entity_values[$this->bundleKey][LanguageInterface::LANGCODE_DEFAULT] : FALSE;

    // Turn the record into an entity class.
    $entities[$id] = new $this->entityClass($entity_values, $this->entityTypeId, $bundle, array_keys($translations[$id]));
  }
  return $entities;
}