Batch powered update hooks

The first thing we are going to look at is update hooks, revisiting of our previous Sports module created in Chapter 8 , The Database API. We will focus on the &$sandbox parameter we didn't use then. 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 the time in individual requests to prevent a PHP timeout, in case we have many records.

So to get us going, here is all the code, and we'll see right after what everything means:

/** * Update all the players to mark them as retired. */ function sports_update_8002(&$sandbox) { $database = \Drupal::database(); if (empty($sandbox)) { $results = $database->query("SELECT ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.