You are here

public function CustomEntityDefinitionUpdateManager::applyUpdates in Entity Update 2.0.x

Applies all the detected valid changes.

File

src/CustomEntityDefinitionUpdateManager.php, line 112

Class

CustomEntityDefinitionUpdateManager
Development version of the entity definition update manager.

Namespace

Drupal\entity_update

Code

public function applyUpdates() {

  // Ensure this works also on Drupal 8.6 and earlier.
  $reflector = new \ReflectionMethod($this->entityDefinitionUpdateManager, 'getChangeList');
  $reflector
    ->setAccessible(TRUE);
  $complete_change_list = $reflector
    ->invoke($this->entityDefinitionUpdateManager);
  if ($complete_change_list) {

    // EntityDefinitionUpdateManagerInterface::getChangeList() only disables
    // the cache and does not invalidate. In case there are changes,
    // explicitly invalidate caches.
    $this->entityTypeManager
      ->clearCachedDefinitions();
    $this->entityFieldManager
      ->clearCachedFieldDefinitions();
  }
  foreach ($complete_change_list as $entity_type_id => $change_list) {

    // Process entity type definition changes before storage definitions ones
    // this is necessary when you change an entity type from non-revisionable
    // to revisionable and at the same time add revisionable fields to the
    // entity type.
    if (!empty($change_list['entity_type'])) {
      $this
        ->doEntityUpdate($change_list['entity_type'], $entity_type_id);
    }

    // Process field storage definition changes.
    if (!empty($change_list['field_storage_definitions'])) {
      $storage_definitions = $this->entityFieldManager
        ->getFieldStorageDefinitions($entity_type_id);
      $original_storage_definitions = $this->entityLastInstalledSchemaRepository
        ->getLastInstalledFieldStorageDefinitions($entity_type_id);
      foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
        $storage_definition = isset($storage_definitions[$field_name]) ? $storage_definitions[$field_name] : NULL;
        $original_storage_definition = isset($original_storage_definitions[$field_name]) ? $original_storage_definitions[$field_name] : NULL;
        $this
          ->doFieldUpdate($change, $storage_definition, $original_storage_definition);
      }
    }
  }
}