You are here

public function EntityUpdateService::applyUpdates in Entity Construction Kit (ECK) 8

Applies all the detected valid changes.

File

src/EntityUpdateService.php, line 96

Class

EntityUpdateService
Class to update entity and field storage for ECK entities.

Namespace

Drupal\eck

Code

public function applyUpdates($eck_entity_type_id) {

  // 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);
  $eck_change_list = array_intersect_key($complete_change_list, [
    $eck_entity_type_id => 1,
    $eck_entity_type_id . '_type' => 1,
  ]);
  if ($eck_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 ($eck_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);
      }
    }
  }
}