/** * 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. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The storage definition of the field whose schema has to be returned. * @param string $table_name * The name of the table columns will be added to. * @param string[] $column_mapping * A mapping of field column names to database column names. * * @return 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. */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = array(); $field_schema = $storage_definition->getSchema(); // Check that the schema does not include forbidden column names. if (array_intersect(array_keys($field_schema['columns']), $this->storage->getTableMapping()->getReservedColumns())) { throw new FieldException("Illegal field column names on {$storage_definition->getName()}"); } $field_name = $storage_definition->getName(); $base_table = $this->storage->getBaseTable(); // 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. However, even those would break in the // case where a new field is added to a table that contains existing rows. // For now, we only hardcode 'not null' to a couple "entity keys", in order // to keep their indexes optimized. // @todo Revisit once we have support for 'initial' in // https://www.drupal.org/node/2346019. $not_null_keys = $this->entityType->getKeys(); // Label fields are not necessarily required. unset($not_null_keys['label']); // 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); } 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); } return $schema; }
/** * Returns 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. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The storage definition of the field whose schema has to be returned. * @param string $table_name * The name of the table columns will be added to. * @param string[] $column_mapping * A mapping of field column names to database column names. * * @return 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. */ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) { $schema = array(); $field_schema = $storage_definition->getSchema(); // Check that the schema does not include forbidden column names. if (array_intersect(array_keys($field_schema['columns']), $this->storage->getTableMapping()->getReservedColumns())) { throw new FieldException(format_string('Illegal field column names on @field_name', array('@field_name' => $storage_definition->getName()))); } $field_name = $storage_definition->getName(); $field_description = $storage_definition->getDescription(); 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]['description'] = $field_description; // Only entity keys are required. $keys = $this->entityType->getKeys() + array('langcode' => 'langcode'); // The label is an entity key, but label fields are not necessarily // required. // 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. unset($keys['label'], $keys['revision']); // Key fields may not be NULL. if (in_array($field_name, $keys)) { $schema['fields'][$schema_field_name]['not null'] = TRUE; } } 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); } return $schema; }
/** * Returns the schema for a single field definition. * * @param array $schema * The table schema to add the field schema to, passed by reference. * @param string $field_name * The name of the field. * @param string[] $column_mapping * A mapping of field column names to database column names. */ protected function addFieldSchema(array &$schema, $field_name, array $column_mapping) { $field_schema = $this->fieldStorageDefinitions[$field_name]->getSchema(); $field_description = $this->fieldStorageDefinitions[$field_name]->getDescription(); 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]['description'] = $field_description; // Only entity keys are required. $keys = $this->entityType->getKeys() + array('langcode' => 'langcode'); // The label is an entity key, but label fields are not necessarily // required. // 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. unset($keys['label'], $keys['revision']); // Key fields may not be NULL. if (in_array($field_name, $keys)) { $schema['fields'][$schema_field_name]['not null'] = TRUE; } } if (!empty($field_schema['indexes'])) { $indexes = $this->getFieldIndexes($field_name, $field_schema, $column_mapping); $schema['indexes'] = array_merge($schema['indexes'], $indexes); } if (!empty($field_schema['unique keys'])) { $unique_keys = $this->getFieldUniqueKeys($field_name, $field_schema, $column_mapping); $schema['unique keys'] = array_merge($schema['unique keys'], $unique_keys); } if (!empty($field_schema['foreign keys'])) { $foreign_keys = $this->getFieldForeignKeys($field_name, $field_schema, $column_mapping); $schema['foreign keys'] = array_merge($schema['foreign keys'], $foreign_keys); } }