You are here

public function SqlContentEntityStorageSchema::onFieldStorageDefinitionDelete in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onFieldStorageDefinitionDelete()

Reacts to the deletion of a field storage definition.

Parameters

\Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition: The field being deleted.

Overrides FieldStorageDefinitionListenerInterface::onFieldStorageDefinitionDelete

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php, line 409
Contains \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema.

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {

  // Only configurable fields currently support purging, so prevent deletion
  // of ones we can't purge if they have existing data.
  // @todo Add purging to all fields: https://www.drupal.org/node/2282119.
  try {
    if (!$storage_definition instanceof FieldStorageConfigInterface && $this->storage
      ->countFieldData($storage_definition, TRUE)) {
      throw new FieldStorageDefinitionUpdateForbiddenException('Unable to delete a field (' . $storage_definition
        ->getName() . ' in ' . $storage_definition
        ->getTargetEntityTypeId() . ' entity) with data that cannot be purged.');
    }
  } catch (DatabaseException $e) {

    // This may happen when changing field storage schema, since we are not
    // able to use a table mapping matching the passed storage definition.
    // @todo Revisit this once we are able to instantiate the table mapping
    //   properly. See https://www.drupal.org/node/2274017.
    return;
  }

  // Retrieve a table mapping which contains the deleted field still.
  $table_mapping = $this->storage
    ->getTableMapping($this->entityManager
    ->getLastInstalledFieldStorageDefinitions($this->entityType
    ->id()));
  if ($table_mapping
    ->requiresDedicatedTableStorage($storage_definition)) {

    // Move the table to a unique name while the table contents are being
    // deleted.
    $table = $table_mapping
      ->getDedicatedDataTableName($storage_definition);
    $new_table = $table_mapping
      ->getDedicatedDataTableName($storage_definition, TRUE);
    $this->database
      ->schema()
      ->renameTable($table, $new_table);
    if ($this->entityType
      ->isRevisionable()) {
      $revision_table = $table_mapping
        ->getDedicatedRevisionTableName($storage_definition);
      $revision_new_table = $table_mapping
        ->getDedicatedRevisionTableName($storage_definition, TRUE);
      $this->database
        ->schema()
        ->renameTable($revision_table, $revision_new_table);
    }
  }

  // @todo Remove when finalizePurge() is invoked from the outside for all
  //   fields: https://www.drupal.org/node/2282119.
  if (!$storage_definition instanceof FieldStorageConfigInterface) {
    $this
      ->performFieldSchemaOperation('delete', $storage_definition);
  }
}