protected function SqlContentEntityStorageSchema::createSharedTableSchema

protected SqlContentEntityStorageSchema::createSharedTableSchema(FieldStorageDefinitionInterface $storage_definition, $only_save = FALSE)

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.

File

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

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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Sql!SqlContentEntityStorageSchema.php/function/SqlContentEntityStorageSchema::createSharedTableSchema/8.1.x