You are here

protected function SqlContentEntityStorageSchema::getEntitySchemaData in Drupal 9

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

Gets entity schema definitions for index and key definitions.

Parameters

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

array $schema: The entity schema array.

Return value

array A stripped down version of the $schema Schema API array containing, for each table, only the key and index definitions not derived from field storage definitions.

3 calls to SqlContentEntityStorageSchema::getEntitySchemaData()
SqlContentEntityStorageSchema::createEntitySchemaIndexes in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Creates the specified entity schema indexes and keys.
SqlContentEntityStorageSchema::requiresEntityStorageSchemaChanges in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Checks if the changes to the entity type requires storage schema changes.
SqlContentEntityStorageSchema::saveEntitySchemaData in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Stores schema data for the given entity type definition.

File

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

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema) {
  $entity_type_id = $entity_type
    ->id();

  // Collect all possible field schema identifiers for shared table fields.
  // These will be used to detect entity schema data in the subsequent loop.
  $field_schema_identifiers = [];
  $table_mapping = $this
    ->getTableMapping($entity_type);
  foreach ($this->fieldStorageDefinitions as $field_name => $storage_definition) {
    if ($table_mapping
      ->allowsSharedTableStorage($storage_definition)) {

      // Make sure both base identifier names and suffixed names are listed.
      $name = $this
        ->getFieldSchemaIdentifierName($entity_type_id, $field_name);
      $field_schema_identifiers[$name] = $name;
      foreach ($storage_definition
        ->getColumns() as $key => $columns) {
        $name = $this
          ->getFieldSchemaIdentifierName($entity_type_id, $field_name, $key);
        $field_schema_identifiers[$name] = $name;
      }
    }
  }

  // Extract entity schema data from the Schema API definition.
  $schema_data = [];
  $keys = [
    'indexes',
    'unique keys',
  ];
  $unused_keys = array_flip([
    'description',
    'fields',
    'foreign keys',
  ]);
  foreach ($schema as $table_name => $table_schema) {
    $table_schema = array_diff_key($table_schema, $unused_keys);
    foreach ($keys as $key) {

      // Exclude data generated from field storage definitions, we will check
      // that separately.
      if ($field_schema_identifiers && !empty($table_schema[$key])) {
        $table_schema[$key] = array_diff_key($table_schema[$key], $field_schema_identifiers);
      }
    }
    $schema_data[$table_name] = array_filter($table_schema);
  }
  return $schema_data;
}