function SqlContentEntityStorage::loadFromDedicatedTables

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::loadFromDedicatedTables()
  2. 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::loadFromDedicatedTables()
  3. 10 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: Flag to indicate whether revisions should be loaded or not.

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 1192

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 = [];
    $ids = [];
    $default_langcodes = [];
    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 = [];
    $definitions = [];
    $table_mapping = $this->getTableMapping();
    foreach ($bundles as $bundle => $v) {
        $definitions[$bundle] = $this->entityFieldManager
            ->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;
            $value_key = !$load_from_revision ? $row->entity_id : $row->revision_id;
            // Field values in default language are stored with
            // LanguageInterface::LANGCODE_DEFAULT as key.
            $langcode = LanguageInterface::LANGCODE_DEFAULT;
            if ($this->langcodeKey && isset($default_langcodes[$value_key]) && $row->langcode != $default_langcodes[$value_key]) {
                $langcode = $row->langcode;
            }
            if (!isset($values[$value_key][$field_name][$langcode])) {
                $values[$value_key][$field_name][$langcode] = [];
            }
            // 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[$value_key][$field_name][$langcode]) < $storage_definition->getCardinality()) {
                    $item = [];
                    // 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[$value_key][$field_name][$langcode][] = $item;
                }
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.