You are here

protected function SqlContentEntityStorage::loadFromSharedTables in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::loadFromSharedTables()

Loads values for fields stored in the shared data tables.

Parameters

array &$values: Associative array of entities values, keyed on the entity ID.

array &$translations: List of translations, keyed on the entity ID.

1 call to SqlContentEntityStorage::loadFromSharedTables()
SqlContentEntityStorage::mapFromStorageRecords in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Maps from storage records to entity objects, and attaches fields.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 503
Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorage.

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function loadFromSharedTables(array &$values, array &$translations) {
  if ($this->dataTable) {

    // If a revision table is available, we need all the properties of the
    // latest revision. Otherwise we fall back to the data table.
    $table = $this->revisionDataTable ?: $this->dataTable;
    $alias = $this->revisionDataTable ? 'revision' : 'data';
    $query = $this->database
      ->select($table, $alias, array(
      'fetch' => \PDO::FETCH_ASSOC,
    ))
      ->fields($alias)
      ->condition($alias . '.' . $this->idKey, array_keys($values), 'IN')
      ->orderBy($alias . '.' . $this->idKey);
    $table_mapping = $this
      ->getTableMapping();
    if ($this->revisionDataTable) {

      // Find revisioned fields that are not entity keys. Exclude the langcode
      // key as the base table holds only the default language.
      $base_fields = array_diff($table_mapping
        ->getFieldNames($this->baseTable), array(
        $this->langcodeKey,
      ));
      $fields = array_diff($table_mapping
        ->getFieldNames($this->revisionDataTable), $base_fields);

      // Find fields that are not revisioned or entity keys. Data fields have
      // the same value regardless of entity revision.
      $data_fields = array_diff($table_mapping
        ->getFieldNames($this->dataTable), $fields, $base_fields);
      if ($data_fields) {
        $fields = array_merge($fields, $data_fields);
        $query
          ->leftJoin($this->dataTable, 'data', "(revision.{$this->idKey} = data.{$this->idKey})");
        $query
          ->fields('data', $data_fields);
      }

      // Get the revision IDs.
      $revision_ids = array();
      foreach ($values as $entity_values) {
        $revision_ids[] = $entity_values[$this->revisionKey][LanguageInterface::LANGCODE_DEFAULT];
      }
      $query
        ->condition('revision.' . $this->revisionKey, $revision_ids, 'IN');
    }
    else {
      $fields = $table_mapping
        ->getFieldNames($this->dataTable);
    }
    $result = $query
      ->execute();
    foreach ($result as $row) {
      $id = $row[$this->idKey];

      // Field values in default language are stored with
      // LanguageInterface::LANGCODE_DEFAULT as key.
      $langcode = empty($row[$this->defaultLangcodeKey]) ? $row[$this->langcodeKey] : LanguageInterface::LANGCODE_DEFAULT;
      $translations[$id][$langcode] = TRUE;
      foreach ($fields as $field_name) {
        $columns = $table_mapping
          ->getColumnNames($field_name);

        // Do not key single-column fields by property name.
        if (count($columns) == 1) {
          $values[$id][$field_name][$langcode] = $row[reset($columns)];
        }
        else {
          foreach ($columns as $property_name => $column_name) {
            $values[$id][$field_name][$langcode][$property_name] = $row[$column_name];
          }
        }
      }
    }
  }
}