/** * {@inheritdoc} */ public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id); unset($definitions[$storage_definition->getName()]); $this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions); }
/** * Gets the SQL schema for a dedicated table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type * (optional) The entity type definition. Defaults to the one returned by * the entity manager. * * @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. * * @see hook_schema() */ protected function getDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, ContentEntityTypeInterface $entity_type = NULL) { $description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $id_definition = $this->fieldStorageDefinitions[$this->entityType->getKey('id')]; if ($id_definition->getType() == 'integer') { $id_schema = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The entity id this data is attached to'); } else { $id_schema = array('type' => 'varchar_ascii', 'length' => 128, 'not null' => TRUE, 'description' => 'The entity id this data is attached to'); } // Define the revision ID schema. if (!$this->entityType->isRevisionable()) { $revision_id_schema = $id_schema; $revision_id_schema['description'] = 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id'; } elseif ($this->fieldStorageDefinitions[$this->entityType->getKey('revision')]->getType() == 'integer') { $revision_id_schema = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The entity revision id this data is attached to'); } else { $revision_id_schema = array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'description' => 'The entity revision id this data is attached to'); } $data_schema = array('description' => $description_current, 'fields' => array('bundle' => array('type' => 'varchar_ascii', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance'), 'deleted' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'description' => 'A boolean indicating whether this data item has been deleted'), 'entity_id' => $id_schema, 'revision_id' => $revision_id_schema, 'langcode' => array('type' => 'varchar_ascii', 'length' => 32, 'not null' => TRUE, 'default' => '', 'description' => 'The language code for this data item.'), 'delta' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The sequence number for this data item, used for multi-value fields')), 'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'), 'indexes' => array('bundle' => array('bundle'), 'revision_id' => array('revision_id'))); // Check that the schema does not include forbidden column names. $schema = $storage_definition->getSchema(); $properties = $storage_definition->getPropertyDefinitions(); $table_mapping = $this->storage->getTableMapping(); if (array_intersect(array_keys($schema['columns']), $table_mapping->getReservedColumns())) { throw new FieldException("Illegal field column names on {$storage_definition->getName()}"); } // Add field columns. foreach ($schema['columns'] as $column_name => $attributes) { $real_name = $table_mapping->getFieldColumnName($storage_definition, $column_name); $data_schema['fields'][$real_name] = $attributes; // A dedicated table only contain rows for actual field values, and no // rows for entities where the field is empty. Thus, we can safely // enforce 'not null' on the columns for the field's required properties. $data_schema['fields'][$real_name]['not null'] = $properties[$column_name]->isRequired(); } // Add indexes. foreach ($schema['indexes'] as $index_name => $columns) { $real_name = $this->getFieldIndexName($storage_definition, $index_name); foreach ($columns as $column_name) { // Indexes can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $data_schema['indexes'][$real_name][] = array($table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1]); } else { $data_schema['indexes'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } } // Add unique keys. foreach ($schema['unique keys'] as $index_name => $columns) { $real_name = $this->getFieldIndexName($storage_definition, $index_name); foreach ($columns as $column_name) { // Unique keys can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $data_schema['unique keys'][$real_name][] = array($table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1]); } else { $data_schema['unique keys'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } } // Add foreign keys. foreach ($schema['foreign keys'] as $specifier => $specification) { $real_name = $this->getFieldIndexName($storage_definition, $specifier); $data_schema['foreign keys'][$real_name]['table'] = $specification['table']; foreach ($specification['columns'] as $column_name => $referenced) { $sql_storage_column = $table_mapping->getFieldColumnName($storage_definition, $column_name); $data_schema['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced; } } $dedicated_table_schema = array($table_mapping->getDedicatedDataTableName($storage_definition) => $data_schema); // If the entity type is revisionable, construct the revision table. $entity_type = $entity_type ?: $this->entityType; if ($entity_type->isRevisionable()) { $revision_schema = $data_schema; $revision_schema['description'] = $description_revision; $revision_schema['primary key'] = array('entity_id', 'revision_id', 'deleted', 'delta', 'langcode'); $revision_schema['fields']['revision_id']['not null'] = TRUE; $revision_schema['fields']['revision_id']['description'] = 'The entity revision id this data is attached to'; $dedicated_table_schema += array($table_mapping->getDedicatedRevisionTableName($storage_definition) => $revision_schema); } return $dedicated_table_schema; }
/** * Generates a safe and unambiguous field table name. * * The method accounts for a maximum table name length of 64 characters, and * takes care of disambiguation. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param bool $revision * TRUE for revision table, FALSE otherwise. * * @return string * The final table name. */ protected function generateFieldTableName(FieldStorageDefinitionInterface $storage_definition, $revision) { $separator = $revision ? '_revision__' : '__'; $table_name = $storage_definition->getTargetEntityTypeId() . $separator . $storage_definition->getName(); // Limit the string to 48 characters, keeping a 16 characters margin for db // prefixes. if (strlen($table_name) > 48) { // Use a shorter separator, a truncated entity_type, and a hash of the // field UUID. $separator = $revision ? '_r__' : '__'; // Truncate to the same length for the current and revision tables. $entity_type = substr($storage_definition->getTargetEntityTypeId(), 0, 34); $field_hash = substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); $table_name = $entity_type . $separator . $field_hash; } return $table_name; }
/** * Creates a new field definition based upon a field storage definition. * * In cases where one needs a field storage definitions to act like full * field definitions, this creates a new field definition based upon the * (limited) information available. That way it is possible to use the field * definition in places where a full field definition is required; e.g., with * widgets or formatters. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition * The field storage definition to base the new field definition upon. * * @return $this */ public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) { return static::create($definition->getType())->setCardinality($definition->getCardinality())->setConstraints($definition->getConstraints())->setCustomStorage($definition->hasCustomStorage())->setDescription($definition->getDescription())->setLabel($definition->getLabel())->setName($definition->getName())->setProvider($definition->getProvider())->setQueryable($definition->isQueryable())->setRequired($definition->isRequired())->setRevisionable($definition->isRevisionable())->setSettings($definition->getSettings())->setTargetEntityTypeId($definition->getTargetEntityTypeId())->setTranslatable($definition->isTranslatable()); }
/** * Checks if the changes to the storage definition requires schema changes. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The updated field storage definition. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original * The original field storage definition. * * @return bool * TRUE if storage schema changes are required, FALSE otherwise. */ protected function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { $storage = $this->entityManager->getStorage($storage_definition->getTargetEntityTypeId()); return $storage instanceof DynamicallyFieldableEntityStorageSchemaInterface && $storage->requiresFieldStorageSchemaChanges($storage_definition, $original); }
/** * {@inheritdoc} */ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = $this->entityTypeManager->getStorage($entity_type_id); if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionDelete($storage_definition); } $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition)); $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); }