SqlContentEntityStorageSchema::deleteSharedTableSchema

protected SqlContentEntityStorageSchema::deleteSharedTableSchema(FieldStorageDefinitionInterface $storage_definition)

Deletes the schema for a field stored in a shared table.

Parameters

\Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition: The storage definition of the field being deleted.

File

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

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

protected function deleteSharedTableSchema(FieldStorageDefinitionInterface $storage_definition) {
  // Make sure any entity index involving this field is dropped.
  $this->deleteEntitySchemaIndexes($this->loadEntitySchemaData($this->entityType), $storage_definition);

  $deleted_field_name = $storage_definition->getName();
  $table_mapping = $this->storage->getTableMapping(
  $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id())
  );
  $column_names = $table_mapping->getColumnNames($deleted_field_name);
  $schema_handler = $this->database->schema();
  $shared_table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames());

  // Iterate over the mapped table to find the ones that host the deleted
  // field schema.
  foreach ($shared_table_names as $table_name) {
    foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
      if ($field_name == $deleted_field_name) {
        $schema = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names);

        // Drop indexes and unique keys first.
        if (!empty($schema['indexes'])) {
          foreach ($schema['indexes'] as $name => $specifier) {
            $schema_handler->dropIndex($table_name, $name);
          }
        }
        if (!empty($schema['unique keys'])) {
          foreach ($schema['unique keys'] as $name => $specifier) {
            $schema_handler->dropUniqueKey($table_name, $name);
          }
        }
        // Drop columns.
        foreach ($column_names as $column_name) {
          $schema_handler->dropField($table_name, $column_name);
        }
        // After deleting the field schema skip to the next table.
        break;
      }
    }
  }

  $this->deleteFieldSchemaData($storage_definition);
}
doc_Drupal
2016-10-29 09:43:36
Comments
Leave a Comment

Please login to continue.