You are here

public function VarbaseEntityDefinitionUpdateManager::applyUpdates in Varbase: The Ultimate Drupal CMS Starter Kit (Bootstrap Ready) 8.7

Same name and namespace in other branches
  1. 8.8 src/Entity/VarbaseEntityDefinitionUpdateManager.php \Drupal\varbase\entity\VarbaseEntityDefinitionUpdateManager::applyUpdates()
  2. 8.6 src/Entity/VarbaseEntityDefinitionUpdateManager.php \Drupal\varbase\entity\VarbaseEntityDefinitionUpdateManager::applyUpdates()

Applies all the detected valid changes.

File

src/Entity/VarbaseEntityDefinitionUpdateManager.php, line 116

Class

VarbaseEntityDefinitionUpdateManager
Varbase Entity Definition Update Manager.

Namespace

Drupal\varbase\entity

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);
      }
    }
  }
}