You are here

public function SqlFieldableEntityTypeListenerTrait::onFieldableEntityTypeUpdate in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlFieldableEntityTypeListenerTrait.php \Drupal\Core\Entity\Sql\SqlFieldableEntityTypeListenerTrait::onFieldableEntityTypeUpdate()
  2. 9 core/lib/Drupal/Core/Entity/Sql/SqlFieldableEntityTypeListenerTrait.php \Drupal\Core\Entity\Sql\SqlFieldableEntityTypeListenerTrait::onFieldableEntityTypeUpdate()
1 call to SqlFieldableEntityTypeListenerTrait::onFieldableEntityTypeUpdate()
SqlContentEntityStorageSchema::onFieldableEntityTypeUpdate in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
Reacts to the update of a fieldable entity type.

File

core/lib/Drupal/Core/Entity/Sql/SqlFieldableEntityTypeListenerTrait.php, line 24

Class

SqlFieldableEntityTypeListenerTrait
Helper methods for EntityTypeListenerInterface.

Namespace

Drupal\Core\Entity\Sql

Code

public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {

  /** @var \Drupal\Core\Entity\EntityStorageInterface $original_storage */
  $original_storage = $this->entityTypeManager
    ->createHandlerInstance($original
    ->getStorageClass(), $original);
  $has_data = $original_storage
    ->hasData();

  // If 'progress' is not set, then this will be the first run of the batch.
  if (!isset($sandbox['progress'])) {

    // We cannot support updating the schema of an entity type from
    // revisionable to non-revisionable or translatable to non-translatable
    // because that can lead to unintended data loss.
    // @todo Add support for these conversions in case there is no data loss.
    //   @see https://www.drupal.org/project/drupal/issues/3024727
    $convert_rev_to_non_rev = $original
      ->isRevisionable() && !$entity_type
      ->isRevisionable();
    $convert_mul_to_non_mul = $original
      ->isTranslatable() && !$entity_type
      ->isTranslatable();
    if ($has_data && ($convert_rev_to_non_rev || $convert_mul_to_non_mul)) {
      throw new EntityStorageException('Converting an entity type from revisionable to non-revisionable or from translatable to non-translatable is not supported.');
    }

    // Check that the fields required by a revisionable entity type exist.
    if ($entity_type
      ->isRevisionable() && !isset($field_storage_definitions[$entity_type
      ->getKey('revision')])) {
      throw new EntityStorageException('Missing revision field.');
    }
    if ($entity_type
      ->isRevisionable() && !isset($field_storage_definitions[$entity_type
      ->getRevisionMetadataKey('revision_default')])) {
      throw new EntityStorageException('Missing revision_default field.');
    }

    // Check that the fields required by a translatable entity type exist.
    if ($entity_type
      ->isTranslatable() && !isset($field_storage_definitions[$entity_type
      ->getKey('langcode')])) {
      throw new EntityStorageException('Missing langcode field.');
    }
    if ($entity_type
      ->isTranslatable() && !isset($field_storage_definitions[$entity_type
      ->getKey('default_langcode')])) {
      throw new EntityStorageException('Missing default_langcode field.');
    }

    // Check that the fields required by a revisionable and translatable
    // entity type exist.
    if ($entity_type
      ->isRevisionable() && $entity_type
      ->isTranslatable() && !isset($field_storage_definitions[$entity_type
      ->getKey('revision_translation_affected')])) {
      throw new EntityStorageException('Missing revision_translation_affected field.');
    }
    $this
      ->preUpdateEntityTypeSchema($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  }

  // Copy data from the original storage to the temporary one.
  if ($has_data) {
    $this
      ->copyData($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  }
  else {

    // If there is no existing data, we still need to run the
    // post-schema-update tasks.
    $sandbox['#finished'] = 1;
  }

  // If the data copying has finished successfully, allow the storage schema
  // to do any required cleanup tasks. For example, this process should take
  // care of transforming the temporary storage into the current storage.
  if ($sandbox['#finished'] == 1) {
    $this
      ->postUpdateEntityTypeSchema($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  }
}