You are here

protected function SqlContentEntityStorage::loadFromDedicatedTables 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::loadFromDedicatedTables()

Loads values of fields stored in dedicated tables for a group of entities.

Parameters

array &$values: An array of values keyed by entity ID.

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

1 call to SqlContentEntityStorage::loadFromDedicatedTables()
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 1072
Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorage.

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function loadFromDedicatedTables(array &$values, $load_from_revision) {
  if (empty($values)) {
    return;
  }

  // Collect entities ids, bundles and languages.
  $bundles = array();
  $ids = array();
  $default_langcodes = array();
  foreach ($values as $key => $entity_values) {
    $bundles[$this->bundleKey ? $entity_values[$this->bundleKey][LanguageInterface::LANGCODE_DEFAULT] : $this->entityTypeId] = TRUE;
    $ids[] = !$load_from_revision ? $key : $entity_values[$this->revisionKey][LanguageInterface::LANGCODE_DEFAULT];
    if ($this->langcodeKey && isset($entity_values[$this->langcodeKey][LanguageInterface::LANGCODE_DEFAULT])) {
      $default_langcodes[$key] = $entity_values[$this->langcodeKey][LanguageInterface::LANGCODE_DEFAULT];
    }
  }

  // Collect impacted fields.
  $storage_definitions = array();
  $definitions = array();
  $table_mapping = $this
    ->getTableMapping();
  foreach ($bundles as $bundle => $v) {
    $definitions[$bundle] = $this->entityManager
      ->getFieldDefinitions($this->entityTypeId, $bundle);
    foreach ($definitions[$bundle] as $field_name => $field_definition) {
      $storage_definition = $field_definition
        ->getFieldStorageDefinition();
      if ($table_mapping
        ->requiresDedicatedTableStorage($storage_definition)) {
        $storage_definitions[$field_name] = $storage_definition;
      }
    }
  }

  // Load field data.
  $langcodes = array_keys($this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL));
  foreach ($storage_definitions as $field_name => $storage_definition) {
    $table = !$load_from_revision ? $table_mapping
      ->getDedicatedDataTableName($storage_definition) : $table_mapping
      ->getDedicatedRevisionTableName($storage_definition);

    // Ensure that only values having valid languages are retrieved. Since we
    // are loading values for multiple entities, we cannot limit the query to
    // the available translations.
    $results = $this->database
      ->select($table, 't')
      ->fields('t')
      ->condition(!$load_from_revision ? 'entity_id' : 'revision_id', $ids, 'IN')
      ->condition('deleted', 0)
      ->condition('langcode', $langcodes, 'IN')
      ->orderBy('delta')
      ->execute();
    foreach ($results as $row) {
      $bundle = $row->bundle;

      // Field values in default language are stored with
      // LanguageInterface::LANGCODE_DEFAULT as key.
      $langcode = LanguageInterface::LANGCODE_DEFAULT;
      if ($this->langcodeKey && isset($default_langcodes[$row->entity_id]) && $row->langcode != $default_langcodes[$row->entity_id]) {
        $langcode = $row->langcode;
      }
      if (!isset($values[$row->entity_id][$field_name][$langcode])) {
        $values[$row->entity_id][$field_name][$langcode] = array();
      }

      // Ensure that records for non-translatable fields having invalid
      // languages are skipped.
      if ($langcode == LanguageInterface::LANGCODE_DEFAULT || $definitions[$bundle][$field_name]
        ->isTranslatable()) {
        if ($storage_definition
          ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || count($values[$row->entity_id][$field_name][$langcode]) < $storage_definition
          ->getCardinality()) {
          $item = array();

          // For each column declared by the field, populate the item from the
          // prefixed database column.
          foreach ($storage_definition
            ->getColumns() as $column => $attributes) {
            $column_name = $table_mapping
              ->getFieldColumnName($storage_definition, $column);

            // Unserialize the value if specified in the column schema.
            $item[$column] = !empty($attributes['serialize']) ? unserialize($row->{$column_name}) : $row->{$column_name};
          }

          // Add the item to the field values for the entity.
          $values[$row->entity_id][$field_name][$langcode][] = $item;
        }
      }
    }
  }
}