Mercurial > hg > cmmr2012-drupal-site
diff vendor/chi-teck/drupal-code-generator/templates/d7/hook/update_N.twig @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/chi-teck/drupal-code-generator/templates/d7/hook/update_N.twig Thu Jul 05 14:24:15 2018 +0000 @@ -0,0 +1,49 @@ +/** + * Implements hook_update_N(). + */ +function {{ machine_name }}_update_N(&$sandbox) { + // For non-multipass updates, the signature can simply be; + // function {{ machine_name }}_update_N() { + + // For most updates, the following is sufficient. + db_add_field('mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE, 'description' => 'My new integer column.')); + + // However, for more complex operations that may take a long time, + // you may hook into Batch API as in the following example. + + // Update 3 users at a time to have an exclamation point after their names. + // (They're really happy that we can do batch API in this hook!) + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['current_uid'] = 0; + // We'll -1 to disregard the uid 0... + $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')->fetchField() - 1; + } + + $users = db_select('users', 'u') + ->fields('u', array('uid', 'name')) + ->condition('uid', $sandbox['current_uid'], '>') + ->range(0, 3) + ->orderBy('uid', 'ASC') + ->execute(); + + foreach ($users as $user) { + $user->name .= '!'; + db_update('users') + ->fields(array('name' => $user->name)) + ->condition('uid', $user->uid) + ->execute(); + + $sandbox['progress']++; + $sandbox['current_uid'] = $user->uid; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + + // To display a message to the user when the update is completed, return it. + // If you do not want to display a completion message, simply return nothing. + return t('The update did what it was supposed to do.'); + + // In case of an error, simply throw an exception with an error message. + throw new DrupalUpdateException('Something went wrong; here is what you should do.'); +}