You are here

protected function SqlContentEntityStorageSchema::createSharedTableSchema 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::createSharedTableSchema()
  2. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::createSharedTableSchema()

Creates the schema for a field stored in a shared table.

Parameters

\Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition: The storage definition of the field being created.

bool $only_save: (optional) Whether to skip modification of database tables and only save the schema data for future comparison. For internal use only. This is used by onEntityTypeCreate() after it has already fully created the shared tables.

3 calls to SqlContentEntityStorageSchema::createSharedTableSchema()
SqlContentEntityStorageSchema::onFieldableEntityTypeCreate in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Reacts to the creation of the fieldable entity type.
SqlContentEntityStorageSchema::postUpdateEntityTypeSchema in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Allows subscribers to do any cleanup necessary after data copying.
SqlContentEntityStorageSchema::updateSharedTableSchema in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Updates the schema for a field stored in a shared table.

File

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

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

protected function createSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, $only_save = FALSE) {
  $created_field_name = $storage_definition
    ->getName();
  $table_mapping = $this
    ->getTableMapping($this->entityType, [
    $storage_definition,
  ]);
  $column_names = $table_mapping
    ->getColumnNames($created_field_name);
  $schema_handler = $this->database
    ->schema();
  $shared_table_names = array_diff($table_mapping
    ->getTableNames(), $table_mapping
    ->getDedicatedTableNames());

  // Iterate over the mapped table to find the ones that will host the created
  // field schema.
  $schema = [];
  foreach ($shared_table_names as $table_name) {
    foreach ($table_mapping
      ->getFieldNames($table_name) as $field_name) {
      if ($field_name == $created_field_name) {

        // Create field columns.
        $schema[$table_name] = $this
          ->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);
        if (!$only_save) {

          // The entity schema needs to be checked because the field schema is
          // potentially incomplete.
          // @todo Fix this in https://www.drupal.org/node/2929120.
          $entity_schema = $this
            ->getEntitySchema($this->entityType);
          foreach ($schema[$table_name]['fields'] as $name => $specifier) {

            // Check if the field is part of the primary keys and pass along
            // this information when adding the field.
            // @see \Drupal\Core\Database\Schema::addField()
            $new_keys = [];
            if (isset($entity_schema[$table_name]['primary key']) && array_intersect($column_names, $entity_schema[$table_name]['primary key'])) {
              $new_keys = [
                'primary key' => $entity_schema[$table_name]['primary key'],
              ];
            }

            // Check if the field exists because it might already have been
            // created as part of the earlier entity type update event.
            if (!$schema_handler
              ->fieldExists($table_name, $name)) {
              $schema_handler
                ->addField($table_name, $name, $specifier, $new_keys);
            }
          }
          if (!empty($schema[$table_name]['indexes'])) {
            foreach ($schema[$table_name]['indexes'] as $name => $specifier) {

              // Check if the index exists because it might already have been
              // created as part of the earlier entity type update event.
              $this
                ->addIndex($table_name, $name, $specifier, $schema[$table_name]);
            }
          }
          if (!empty($schema[$table_name]['unique keys'])) {
            foreach ($schema[$table_name]['unique keys'] as $name => $specifier) {
              $schema_handler
                ->addUniqueKey($table_name, $name, $specifier);
            }
          }
        }

        // After creating the field schema skip to the next table.
        break;
      }
    }
  }
  $this
    ->saveFieldSchemaData($storage_definition, $schema);
  if (!$only_save) {

    // Make sure any entity index involving this field is re-created if
    // needed.
    $entity_schema = $this
      ->getEntitySchema($this->entityType);
    $this
      ->createEntitySchemaIndexes($entity_schema, $storage_definition);

    // Store the updated entity schema.
    $this
      ->saveEntitySchemaData($this->entityType, $entity_schema);
  }
}