You are here

protected function SqlContentEntityStorageSchema::preUpdateEntityTypeSchema in Drupal 10

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

Allows subscribers to prepare their schema before data copying.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The updated entity type definition.

\Drupal\Core\Entity\EntityTypeInterface $original: The original entity type definition.

\Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions: The updated field storage definitions, including possibly new ones.

\Drupal\Core\Field\FieldStorageDefinitionInterface[] $original_field_storage_definitions: The original field storage definitions.

array &$sandbox: (optional) A sandbox array provided by a hook_update_N() implementation or a Batch API callback. If the entity schema update requires a data migration, this parameter is mandatory. Defaults to NULL.

Overrides SqlFieldableEntityTypeListenerTrait::preUpdateEntityTypeSchema

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php, line 452

Class

SqlContentEntityStorageSchema
Defines a schema handler that supports revisionable, translatable entities.

Namespace

Drupal\Core\Entity\Sql

Code

protected function preUpdateEntityTypeSchema(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
  $temporary_prefix = static::getTemporaryTableMappingPrefix($entity_type, $field_storage_definitions);
  $sandbox['temporary_table_mapping'] = $this->storage
    ->getCustomTableMapping($entity_type, $field_storage_definitions, $temporary_prefix);
  $sandbox['new_table_mapping'] = $this->storage
    ->getCustomTableMapping($entity_type, $field_storage_definitions);
  $sandbox['original_table_mapping'] = $this->storage
    ->getCustomTableMapping($original, $original_field_storage_definitions);
  $backup_prefix = static::getTemporaryTableMappingPrefix($original, $original_field_storage_definitions, 'old_');
  $sandbox['backup_table_mapping'] = $this->storage
    ->getCustomTableMapping($original, $original_field_storage_definitions, $backup_prefix);
  $sandbox['backup_prefix_key'] = substr($backup_prefix, 4);
  $sandbox['backup_request_time'] = \Drupal::time()
    ->getRequestTime();

  // Create temporary tables based on the new entity type and field storage
  // definitions.
  $temporary_table_names = array_combine($this
    ->getTableNames($entity_type, $field_storage_definitions, $sandbox['new_table_mapping']), $this
    ->getTableNames($entity_type, $field_storage_definitions, $sandbox['temporary_table_mapping']));
  $this->entityType = $entity_type;
  $this->fieldStorageDefinitions = $field_storage_definitions;

  // Update the storage's entity type and field storage definitions because
  // ::getEntitySchema() and ::getSharedTableFieldSchema() overrides are
  // retrieving table names from these definitions.
  $this->storage
    ->setEntityType($entity_type);
  $this->storage
    ->setFieldStorageDefinitions($field_storage_definitions);
  $this->storage
    ->setTableMapping($sandbox['new_table_mapping']);
  $schema = $this
    ->getEntitySchema($entity_type, TRUE);
  $sandbox['new_entity_schema'] = $schema;

  // Filter out tables which are not part of the table mapping.
  $schema = array_intersect_key($schema, $temporary_table_names);

  // Create entity tables.
  foreach ($schema as $table_name => $table_schema) {
    $this->database
      ->schema()
      ->createTable($temporary_table_names[$table_name], $table_schema);
  }

  // Create dedicated field tables.
  foreach ($field_storage_definitions as $field_storage_definition) {
    if ($sandbox['temporary_table_mapping']
      ->requiresDedicatedTableStorage($field_storage_definition)) {
      $schema = $this
        ->getDedicatedTableSchema($field_storage_definition, $entity_type);

      // Filter out tables which are not part of the table mapping.
      $schema = array_intersect_key($schema, $temporary_table_names);
      foreach ($schema as $table_name => $table_schema) {
        $this->database
          ->schema()
          ->createTable($temporary_table_names[$table_name], $table_schema);
      }
    }
  }

  // Restore the original definitions and table mapping so the data copying
  // step can load existing data properly.
  $this->storage
    ->setEntityType($original);
  $this->storage
    ->setFieldStorageDefinitions($original_field_storage_definitions);
  $this->storage
    ->setTableMapping($sandbox['original_table_mapping']);

  // Store the temporary table name mappings for later reuse.
  $sandbox['temporary_table_names'] = $temporary_table_names;
}