You are here

protected function CiviEntityStorage::loadFromDedicatedTables in CiviCRM Entity 8.3

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

Parameters

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

Throws

\Drupal\Core\Entity\Sql\SqlContentEntityStorageException

Overrides SqlContentEntityStorage::loadFromDedicatedTables

1 call to CiviEntityStorage::loadFromDedicatedTables()
CiviEntityStorage::prepareLoadedEntity in src/CiviEntityStorage.php
Prepares a loaded entity.

File

src/CiviEntityStorage.php, line 496

Class

CiviEntityStorage
Defines entity class for external CiviCRM entities.

Namespace

Drupal\civicrm_entity

Code

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

  // Collect impacted fields.
  $storage_definitions = [];
  $table_mapping = $this
    ->getTableMapping();
  $definitions = $this
    ->getEntityFieldManager()
    ->getFieldDefinitions($this->entityTypeId, $this->entityTypeId);
  foreach ($definitions 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 = $table_mapping
      ->getDedicatedDataTableName($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('entity_id', [
      $values[$this
        ->getEntityType()
        ->getKey('id')],
    ], 'IN')
      ->condition('deleted', 0)
      ->condition('langcode', $langcodes, 'IN')
      ->orderBy('delta')
      ->execute();
    foreach ($results as $row) {
      if (!isset($values[$field_name])) {
        $values[$field_name] = [];
      }
      if ($storage_definition
        ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || count($values[$field_name]) < $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[$field_name][] = $item;
      }
    }
  }
}