function SqlContentEntityStorageSchema::preUpdateEntityTypeSchema

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::preUpdateEntityTypeSchema()
  2. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::preUpdateEntityTypeSchema()
  3. 11.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::preUpdateEntityTypeSchema()

Overrides SqlFieldableEntityTypeListenerTrait::preUpdateEntityTypeSchema

File

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

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;
}

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