SqlContentEntityStorageSchema::onEntityTypeUpdate

public SqlContentEntityStorageSchema::onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original)

Reacts to the update of the entity type.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The updated entity type definition.

\Drupal\Core\Entity\EntityTypeInterface $original: The original entity type definition.

Overrides EntityTypeListenerInterface::onEntityTypeUpdate

File

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

Class

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

Namespace

Drupal\Core\Entity\Sql

Code

public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  $this->checkEntityType($entity_type);
  $this->checkEntityType($original);

  // If no schema changes are needed, we don't need to do anything.
  if (!$this->requiresEntityStorageSchemaChanges($entity_type, $original)) {
    return;
  }

  // If a migration is required, we can't proceed.
  if ($this->requiresEntityDataMigration($entity_type, $original)) {
    throw new EntityStorageException('The SQL storage cannot change the schema for an existing entity type (' . $entity_type->id() . ') with data.');
  }

  // If we have no data just recreate the entity schema from scratch.
  if ($this->isTableEmpty($this->storage->getBaseTable())) {
    if ($this->database->supportsTransactionalDDL()) {
      // If the database supports transactional DDL, we can go ahead and rely
      // on it. If not, we will have to rollback manually if something fails.
      $transaction = $this->database->startTransaction();
    }
    try {
      $this->onEntityTypeDelete($original);
      $this->onEntityTypeCreate($entity_type);
    }
    catch (\Exception $e) {
      if ($this->database->supportsTransactionalDDL()) {
        $transaction->rollback();
      }
      else {
        // Recreate original schema.
        $this->onEntityTypeCreate($original);
      }
      throw $e;
    }
  }
  else {
    // Drop original indexes and unique keys.
    $this->deleteEntitySchemaIndexes($this->loadEntitySchemaData($entity_type));

    // Create new indexes and unique keys.
    $entity_schema = $this->getEntitySchema($entity_type, TRUE);
    $this->createEntitySchemaIndexes($entity_schema);

    // Store the updated entity schema.
    $this->saveEntitySchemaData($entity_type, $entity_schema);
  }
}
doc_Drupal
2016-10-29 09:43:42
Comments
Leave a Comment

Please login to continue.