You are here

public static function EntityUpdate::entityUpdateDataBackupDel in Entity Update 2.0.x

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

Backup and delete entities before an update.

Return value

bool Has Entity data

4 calls to EntityUpdate::entityUpdateDataBackupDel()
drush_entity_update in drush/entity_update.drush8.inc
Call back function of entity-update.
EntityUpdate::safeUpdateEntityType in src/EntityUpdate.php
Update an entity type.
EntityUpdate::safeUpdateFields in src/EntityUpdate.php
Update fields.
EntityUpdateFunctionsTest::testEntityUpdateAdvanced in tests/src/Functional/EntityUpdateFunctionsTest.php
Entity advanced update simulation.

File

src/EntityUpdate.php, line 180

Class

EntityUpdate
EntityUpdate Main Class.

Namespace

Drupal\entity_update

Code

public static function entityUpdateDataBackupDel($entity_change_summerys, $force_type = NULL) {

  // Force a type to backup and delete even no updates.
  if (empty($entity_change_summerys) && $force_type) {
    $entity_change_summerys[$force_type] = 1;
  }

  // Get Database connection.
  $con = Database::getConnection();
  $excludes = EntityUpdateHelper::getConfig()
    ->get('excludes');
  $flg_has_data = FALSE;

  // Read and backup data into entity_update entity..
  EntityUpdatePrint::drushPrint("Read and backup data");
  foreach ($entity_change_summerys as $entity_type_id => $entity_type_changes) {

    // Check entity types excludes.
    if (empty($excludes[$entity_type_id])) {
      EntityUpdatePrint::drushPrint(" - Reading : {$entity_type_id}");
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entity_type_id)
        ->loadMultiple();
      foreach ($entities as $entity_id => $entity) {
        $entity_type = $entity
          ->getEntityType();

        // Backup entity to DB.
        $con
          ->insert('entity_update')
          ->fields([
          'entity_type' => $entity_type_id,
          'entity_id' => $entity_id,
          'entity_class' => $entity_type
            ->getClass(),
          'status' => 0,
          'data' => Json::encode($entity
            ->toArray()),
        ])
          ->execute();

        // Delete Entity.
        $entity
          ->delete();
        $flg_has_data = TRUE;
      }
      EntityUpdatePrint::drushLog("Backup ok", 'ok');
    }
    else {

      // Exclude by config.
      EntityUpdatePrint::drushLog("Deletation of {$entity_type_id} is excluded by config.", 'cancel');
    }
  }
  return $flg_has_data;
}