Batch-powered update hooks
The first thing we are going to look at is post update hooks, revisiting our previous Sports module created in Chapter 8, The Database API. We will focus on the &$sandbox
, which is present in both the update hook (hook_update_N()
) and the post update hooks (hook_post_update_NAME()
). The goal is to run an update on each of our records in the players
table and mark them as retired. The point is to illustrate how we can process each of these records one at a time in individual requests to prevent a PHP timeout. This is handy if we have many records.
So to get us going, here is all the code, and we'll see right after what everything means. If you remember, this will go in the sports.post_updates.php
file of our module:
/** * Update all the players to mark them as retired. */ function sports_post_update_retire_players(&$sandbox) { Â Â $database = \Drupal::database(); Â Â if (empty($sandbox)) { Â Â Â Â ...