You are here

protected function SqlContentEntityStorageSchema::createSharedTableSchema in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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.

2 calls to SqlContentEntityStorageSchema::createSharedTableSchema()
SqlContentEntityStorageSchema::onEntityTypeCreate in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Reacts to the creation of the entity type.
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 1129
Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema.

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->storage
    ->getTableMapping();
  $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 = array();
  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) {
          foreach ($schema[$table_name]['fields'] as $name => $specifier) {

            // 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);
            }
          }
          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.
    $this
      ->createEntitySchemaIndexes($this
      ->getEntitySchema($this->entityType), $storage_definition);
  }
}