function comment_notify_update_8004 in Comment Notify 8
Migrate the users preferences to the Drupal's users_data table.
File
- ./
comment_notify.install, line 159 - Comment_notify.install.
Code
function comment_notify_update_8004(&$sandbox) {
$database = \Drupal::database();
$user_data = \Drupal::service('user.data');
if (!isset($sandbox['current'])) {
// Set batch ops sandbox.
$sandbox['current'] = 0;
$sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
$select = $database
->select('comment_notify_user_settings', 'c');
$sandbox['max'] = $select
->countQuery()
->execute()
->fetchField();
}
$select = $database
->select('comment_notify_user_settings', 'c');
$settings = $select
->fields('c', [
'uid',
'entity_notify',
'comment_notify',
])
->range($sandbox['current'], $sandbox['limit'])
->orderBy('uid', 'ASC')
->execute()
->fetchAll();
foreach ($settings as $row) {
$user_data
->set('comment_notify', $row->uid, 'entity_notify', $row->entity_notify);
$user_data
->set('comment_notify', $row->uid, 'comment_notify', $row->comment_notify);
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current'] / $sandbox['max'];
if ($sandbox['#finished'] >= 1) {
// Delete the old comment_notify_user_settings table.
$database
->schema()
->dropTable('comment_notify_user_settings');
return t('The comment notify user settings has been migrated to the user data table.');
}
}