protected function DatabaseStorageControllerNG::attachPropertyData

Attaches property data in all languages for translatable properties.

Parameters

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

int $revision_id: (optional) The revision to be loaded. Defaults to FALSE.

1 call to DatabaseStorageControllerNG::attachPropertyData()
DatabaseStorageControllerNG::mapFromStorageRecords in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php
Maps from storage records to entity objects.

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php, line 279
Contains \Drupal\Core\Entity\DatabaseStorageControllerNG.

Class

DatabaseStorageControllerNG
Implements Field API specific enhancements to the DatabaseStorageController class.

Namespace

Drupal\Core\Entity

Code

protected function attachPropertyData(array &$entities, $revision_id = FALSE) {
  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->revisionTable ?: $this->dataTable;
    $query = $this->database
      ->select($table, 'data', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('data')
      ->condition($this->idKey, array_keys($entities))
      ->orderBy('data.' . $this->idKey);
    if ($this->revisionTable) {
      if ($revision_id) {
        $query
          ->condition($this->revisionKey, $revision_id);
      }
      else {

        // Get the revision IDs.
        $revision_ids = array();
        foreach ($entities as $id => $values) {
          $revision_ids[] = $values[$this->revisionKey];
        }
        $query
          ->condition($this->revisionKey, $revision_ids);
      }
    }
    $data = $query
      ->execute();
    $field_definition = $this
      ->getFieldDefinitions(array());
    if ($this->revisionTable) {
      $data_fields = array_flip(array_diff(drupal_schema_fields_sql($this->entityInfo['revision_table']), drupal_schema_fields_sql($this->entityInfo['base_table'])));
    }
    else {
      $data_fields = array_flip(drupal_schema_fields_sql($this->entityInfo['data_table']));
    }
    foreach ($data as $values) {
      $id = $values[$this->idKey];

      // Field values in default language are stored with
      // Language::LANGCODE_DEFAULT as key.
      $langcode = empty($values['default_langcode']) ? $values['langcode'] : Language::LANGCODE_DEFAULT;
      foreach ($field_definition as $name => $definition) {

        // Set only translatable properties, unless we are dealing with a
        // revisable entity, in which case we did not load the untranslatable
        // data before.
        $translatable = !empty($definition['translatable']);
        if (isset($data_fields[$name]) && ($this->revisionTable || $translatable)) {
          $entities[$id][$name][$langcode] = $values[$name];
        }
      }
    }
    foreach ($entities as $id => $values) {
      $bundle = $this->bundleKey ? $values[$this->bundleKey][Language::LANGCODE_DEFAULT] : FALSE;

      // Turn the record into an entity class.
      $entities[$id] = new $this->entityClass($values, $this->entityType, $bundle);
    }
  }
}