You are here

protected function SqlContentEntityStorage::readFieldItemsToPurge in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::readFieldItemsToPurge()
  2. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::readFieldItemsToPurge()

Reads values to be purged for a single field.

This method is called during field data purge, on fields for which onFieldDefinitionDelete() has previously run.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

$batch_size: The maximum number of field data records to purge before returning.

Return value

\Drupal\Core\Field\FieldItemListInterface[] An array of field item lists, keyed by entity revision id.

Overrides ContentEntityStorageBase::readFieldItemsToPurge

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 1632

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definition, $batch_size) {

  // Check whether the whole field storage definition is gone, or just some
  // bundle fields.
  $storage_definition = $field_definition
    ->getFieldStorageDefinition();
  $table_mapping = $this
    ->getTableMapping();
  $table_name = $table_mapping
    ->getDedicatedDataTableName($storage_definition, $storage_definition
    ->isDeleted());

  // Get the entities which we want to purge first.
  $entity_query = $this->database
    ->select($table_name, 't', [
    'fetch' => \PDO::FETCH_ASSOC,
  ]);
  $or = $entity_query
    ->orConditionGroup();
  foreach ($storage_definition
    ->getColumns() as $column_name => $data) {
    $or
      ->isNotNull($table_mapping
      ->getFieldColumnName($storage_definition, $column_name));
  }
  $entity_query
    ->distinct(TRUE)
    ->fields('t', [
    'entity_id',
  ])
    ->condition('bundle', $field_definition
    ->getTargetBundle())
    ->range(0, $batch_size);

  // Create a map of field data table column names to field column names.
  $column_map = [];
  foreach ($storage_definition
    ->getColumns() as $column_name => $data) {
    $column_map[$table_mapping
      ->getFieldColumnName($storage_definition, $column_name)] = $column_name;
  }
  $entities = [];
  $items_by_entity = [];
  foreach ($entity_query
    ->execute() as $row) {
    $item_query = $this->database
      ->select($table_name, 't', [
      'fetch' => \PDO::FETCH_ASSOC,
    ])
      ->fields('t')
      ->condition('entity_id', $row['entity_id'])
      ->condition('deleted', 1)
      ->orderBy('delta');
    foreach ($item_query
      ->execute() as $item_row) {
      if (!isset($entities[$item_row['revision_id']])) {

        // Create entity with the right revision id and entity id combination.
        $item_row['entity_type'] = $this->entityTypeId;

        // @todo: Replace this by an entity object created via an entity
        // factory, see https://www.drupal.org/node/1867228.
        $entities[$item_row['revision_id']] = _field_create_entity_from_ids((object) $item_row);
      }
      $item = [];
      foreach ($column_map as $db_column => $field_column) {
        $item[$field_column] = $item_row[$db_column];
      }
      $items_by_entity[$item_row['revision_id']][] = $item;
    }
  }

  // Create field item objects and return.
  foreach ($items_by_entity as $revision_id => $values) {
    $entity_adapter = $entities[$revision_id]
      ->getTypedData();
    $items_by_entity[$revision_id] = \Drupal::typedDataManager()
      ->create($field_definition, $values, $field_definition
      ->getName(), $entity_adapter);
  }
  return $items_by_entity;
}