You are here

private static function EntityUpdate::safeUpdateEntityType in Entity Update 2.0.x

Same name and namespace in other branches
  1. 8 src/EntityUpdate.php \Drupal\entity_update\EntityUpdate::safeUpdateEntityType()

Update an entity type.

1 call to EntityUpdate::safeUpdateEntityType()
EntityUpdate::safeUpdateMain in src/EntityUpdate.php
Update all entities.

File

src/EntityUpdate.php, line 294

Class

EntityUpdate
EntityUpdate Main Class.

Namespace

Drupal\entity_update

Code

private static function safeUpdateEntityType(EntityTypeInterface $entity_type) {

  // Get entity change summerys.
  $entity_change_summerys = self::getEntityTypesToUpdate($entity_type
    ->id());
  $flg_done = FALSE;
  try {

    // Get entity update manager.
    $update_manager = entity_update_get_entity_definition_update_manager();
    $complete_change_list = $update_manager
      ->getChangeList();
    if ($complete_change_list) {
      $update_manager->entityTypeManager
        ->clearCachedDefinitions();
    }
    foreach ($complete_change_list as $entity_type_id => $change_list) {

      // Update selected entity type only.
      if ($entity_type_id === $entity_type
        ->id()) {

        // Check if has a field to install.
        $flg_has_install = FALSE;
        foreach ($entity_change_summerys[$entity_type_id] as $entity_change_summ) {
          if (strstr($entity_change_summ, "uninstalled")) {

            // Has fields to un install (Nothig todo at the moment).
          }
          else {
            $flg_has_install = TRUE;
          }
        }

        // Backup and delete entities data if no fields to install.
        if (!$flg_has_install) {
          $entity_change_summerys = [
            $entity_type_id => 1,
          ];
          $flg_has_data = self::entityUpdateDataBackupDel($entity_change_summerys);
        }
        else {
          $flg_has_data = FALSE;
        }

        // Process entity types.
        if (!empty($change_list['entity_type'])) {

          // TODO : Backup and Restore data via SQL.
          $update_manager
            ->doEntityUpdate($change_list['entity_type'], $entity_type_id);
        }

        // Process field storage definition changes.
        if (!empty($change_list['field_storage_definitions'])) {
          $storage_definitions = $update_manager->entityFieldManager
            ->getFieldStorageDefinitions($entity_type_id);
          $original_storage_definitions = $update_manager->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;
            $update_manager
              ->doFieldUpdate($change, $storage_definition, $original_storage_definition);
          }
        }

        // Re create if necessary.
        if ($flg_has_data) {
          $result = self::entityUpdateDataRestore();
          if ($result) {
            self::cleanupEntityBackup();
          }
        }
        $flg_done = TRUE;
      }
    }
  } catch (Exception $e) {
    EntityUpdatePrint::drushLog($e
      ->getMessage(), 'error');
    return FALSE;
  }
  return $flg_done;
}