SqlContentEntityStorageSchema::getEntitySchemaData

protected SqlContentEntityStorageSchema::getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema)

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.

File

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

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 = [];
  $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
  $table_mapping = $this->storage->getTableMapping($storage_definitions);
  foreach ($storage_definitions 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;
}
doc_Drupal
2016-10-29 09:43:38
Comments
Leave a Comment

Please login to continue.