You are here

function bibcite_entity_update_8015 in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/bibcite_entity.install \bibcite_entity_update_8015()

Fill Revision create time and Revision user fields, which are left empty after making Reference entity type revisionable, by values from Created and Authored by fields respectively.

File

modules/bibcite_entity/bibcite_entity.install, line 624
Module installation hooks implementation.

Code

function bibcite_entity_update_8015(&$sandbox) {
  $database = \Drupal::database();
  $type_manager = \Drupal::entityTypeManager();
  $table_name = $type_manager
    ->getStorage('bibcite_reference')
    ->getEntityType()
    ->getRevisionTable();
  if (!isset($sandbox['total'])) {
    $sandbox['processed'] = 0;
    $sandbox['current_id'] = 0;
    $count_query = $database
      ->select($table_name);
    $count_condition = $count_query
      ->orConditionGroup()
      ->isNull('revision_created')
      ->isNull('revision_user');
    $count_query
      ->condition($count_condition);
    $count_query = $count_query
      ->countQuery();
    $sandbox['total'] = $count_query
      ->execute()
      ->fetchField();
  }
  $select_query = $database
    ->select($table_name, 'brr');
  $select_query
    ->fields('brr', [
    'revision_id',
    'created',
    'uid',
    'revision_created',
    'revision_user',
  ]);
  $select_condition = $select_query
    ->orConditionGroup()
    ->isNull('revision_created')
    ->isNull('revision_user');
  $select_query
    ->condition($select_condition);
  $select_query
    ->condition('revision_id', $sandbox['current_id'], '>');
  $select_query
    ->orderBy('revision_id');
  $select_query
    ->range(0, 50);
  $rows = $select_query
    ->execute()
    ->fetchAll();
  if ($rows) {
    foreach ($rows as $row) {
      $revision_created = isset($row->revision_created) ? $row->revision_created : $row->created;
      $revision_user = isset($row->revision_user) ? $row->revision_user : $row->uid;
      $database
        ->update($table_name)
        ->fields([
        'revision_created' => $revision_created,
        'revision_user' => $revision_user,
      ])
        ->condition('revision_id', $row->revision_id)
        ->execute();
      $sandbox['current_id'] = $row->revision_id;
    }
    $sandbox['processed'] += count($rows);
    $sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
  }
  else {
    $sandbox['#finished'] = 1;
  }
}