function SqlContentEntityStorageSchema::getSharedTableFieldSchema

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSharedTableFieldSchema()
  2. 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSharedTableFieldSchema()
  3. 11.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSharedTableFieldSchema()

Gets the schema for a single field definition.

Entity types may override this method in order to optimize the generated schema for given field. While all optimizations that apply to a single field have to be added here, all cross-field optimizations should be via SqlContentEntityStorageSchema::getEntitySchema() instead; e.g., an index spanning multiple fields.

Parameters

\Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition: The storage definition of the field whose schema has to be returned.

string $table_name: The name of the table columns will be added to.

string[] $column_mapping: A mapping of field column names to database column names.

Return value

array The schema definition for the table with the following keys:

  • fields: The schema definition for the each field columns.
  • indexes: The schema definition for the indexes.
  • unique keys: The schema definition for the unique keys.
  • foreign keys: The schema definition for the foreign keys.

Throws

\Drupal\Core\Field\FieldException Exception thrown if the schema contains reserved column names or if the initial values definition is invalid.

13 calls to SqlContentEntityStorageSchema::getSharedTableFieldSchema()
BlockContentStorageSchema::getSharedTableFieldSchema in core/modules/block_content/src/BlockContentStorageSchema.php
Gets the schema for a single field definition.
CommentStorageSchema::getSharedTableFieldSchema in core/modules/comment/src/CommentStorageSchema.php
Gets the schema for a single field definition.
EntityTestUpdateStorageSchema::getSharedTableFieldSchema in core/modules/system/tests/modules/entity_test_update/src/EntityTestUpdateStorageSchema.php
Gets the schema for a single field definition.
FileStorageSchema::getSharedTableFieldSchema in core/modules/file/src/FileStorageSchema.php
Gets the schema for a single field definition.
MenuLinkContentStorageSchema::getSharedTableFieldSchema in core/modules/menu_link_content/src/MenuLinkContentStorageSchema.php
Gets the schema for a single field definition.

... See full list

8 methods override SqlContentEntityStorageSchema::getSharedTableFieldSchema()
BlockContentStorageSchema::getSharedTableFieldSchema in core/modules/block_content/src/BlockContentStorageSchema.php
Gets the schema for a single field definition.
CommentStorageSchema::getSharedTableFieldSchema in core/modules/comment/src/CommentStorageSchema.php
Gets the schema for a single field definition.
EntityTestUpdateStorageSchema::getSharedTableFieldSchema in core/modules/system/tests/modules/entity_test_update/src/EntityTestUpdateStorageSchema.php
Gets the schema for a single field definition.
FileStorageSchema::getSharedTableFieldSchema in core/modules/file/src/FileStorageSchema.php
Gets the schema for a single field definition.
MenuLinkContentStorageSchema::getSharedTableFieldSchema in core/modules/menu_link_content/src/MenuLinkContentStorageSchema.php
Gets the schema for a single field definition.

... See full list

File

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

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
    $schema = [];
    $table_mapping = $this->getTableMapping($this->entityType, [
        $storage_definition,
    ]);
    $field_schema = $storage_definition->getSchema();
    // Check that the schema does not include forbidden column names.
    if (array_intersect(array_keys($field_schema['columns']), $table_mapping->getReservedColumns())) {
        throw new FieldException("Illegal field column names on {$storage_definition->getName()}");
    }
    $field_name = $storage_definition->getName();
    $base_table = $this->storage
        ->getBaseTable();
    $revision_table = $this->storage
        ->getRevisionTable();
    // Define the initial values, if any.
    $initial_value = $initial_value_from_field = [];
    $storage_definition_is_new = empty($this->loadFieldSchemaData($storage_definition));
    if ($storage_definition_is_new && $storage_definition instanceof BaseFieldDefinition && $table_mapping->allowsSharedTableStorage($storage_definition)) {
        if (($initial_storage_value = $storage_definition->getInitialValue()) && !empty($initial_storage_value)) {
            // We only support initial values for fields that are stored in shared
            // tables (i.e. single-value fields).
            // @todo Implement initial value support for multi-value fields in
            //   https://www.drupal.org/node/2883851.
            $initial_value = reset($initial_storage_value);
        }
        if ($initial_value_field_name = $storage_definition->getInitialValueFromField()) {
            // Check that the field used for populating initial values is valid.
            if (!isset($this->fieldStorageDefinitions[$initial_value_field_name])) {
                throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field {$initial_value_field_name} does not exist.");
            }
            if ($storage_definition->getType() !== $this->fieldStorageDefinitions[$initial_value_field_name]
                ->getType()) {
                throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field types do not match.");
            }
            if (!$table_mapping->allowsSharedTableStorage($this->fieldStorageDefinitions[$initial_value_field_name])) {
                throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: Both fields have to be stored in the shared entity tables.");
            }
            $initial_value_from_field = $table_mapping->getColumnNames($initial_value_field_name);
        }
    }
    // A shared table contains rows for entities where the field is empty
    // (since other fields stored in the same table might not be empty), thus
    // the only columns that can be 'not null' are those for required
    // properties of required fields. For now, we only hardcode 'not null' to a
    // few "entity keys", in order to keep their indexes optimized.
    // @todo Fix this in https://www.drupal.org/node/2841291.
    $not_null_keys = $this->entityType
        ->getKeys();
    // Label and the 'revision_translation_affected' fields are not necessarily
    // required.
    unset($not_null_keys['label'], $not_null_keys['revision_translation_affected']);
    // Because entity ID and revision ID are both serial fields in the base and
    // revision table respectively, the revision ID is not known yet, when
    // inserting data into the base table. Instead the revision ID in the base
    // table is updated after the data has been inserted into the revision
    // table. For this reason the revision ID field cannot be marked as NOT
    // NULL.
    if ($table_name == $base_table) {
        unset($not_null_keys['revision']);
    }
    foreach ($column_mapping as $field_column_name => $schema_field_name) {
        $column_schema = $field_schema['columns'][$field_column_name];
        $schema['fields'][$schema_field_name] = $column_schema;
        $schema['fields'][$schema_field_name]['not null'] = in_array($field_name, $not_null_keys);
        // Use the initial value of the field storage, if available.
        if ($initial_value && isset($initial_value[$field_column_name])) {
            $schema['fields'][$schema_field_name]['initial'] = SqlContentEntityStorageSchema::castValue($column_schema, $initial_value[$field_column_name]);
        }
        if (!empty($initial_value_from_field)) {
            $schema['fields'][$schema_field_name]['initial_from_field'] = $initial_value_from_field[$field_column_name];
        }
    }
    if (!empty($field_schema['indexes'])) {
        $schema['indexes'] = $this->getFieldIndexes($field_name, $field_schema, $column_mapping);
    }
    if (!empty($field_schema['unique keys'])) {
        $schema['unique keys'] = $this->getFieldUniqueKeys($field_name, $field_schema, $column_mapping);
    }
    if (!empty($field_schema['foreign keys'])) {
        $schema['foreign keys'] = $this->getFieldForeignKeys($field_name, $field_schema, $column_mapping);
    }
    // Process the 'id' and 'revision' entity keys for the base and revision
    // tables.
    if ($table_name === $base_table && $field_name === $this->entityType
        ->getKey('id') || $table_name === $revision_table && $field_name === $this->entityType
        ->getKey('revision')) {
        $this->processIdentifierSchema($schema, $field_name);
    }
    return $schema;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.