Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In Drupal 8 there are two ways for a module to run configuration/schema/database updates. One is by creating a hook_update_N hook in your module's .install file. The other way is new to Drupal 8 and is to create a hook_post_update_NAME hook in your module's .post_update.php file.
Problems:
- The documentation for these hooks do not mention the other at all.
- The update API documentation does not mention even the existence of post_update hooks.
- The only public discussion I could find about this is this DrupalCon Vienna talk from 2017, which mentions a "do's" and "don't"s list for what to put in update hooks, but no clear rules around it.
Proposed resolution
First, we need to establish hard and fast rules about these hooks. I don't know what they are. I generally just understand that it's not safe to put certain changes in hook_update_n(). But if it were as simple as that, then why not just use post_update for everything?
Comment | File | Size | Author |
---|---|---|---|
#5 | 2020-09-24_13-39_1.png | 661.42 KB | Elijah Lynn |
#5 | 2020-09-24_13-35_1.png | 716.5 KB | Elijah Lynn |
Comments
Comment #2
larowlanYes to this. I don't know half the time either and keep hassling jibran for confirmation
Comment #3
jibranFor me, there are three things to remember:
https://www.drupal.org/node/3034742 is a very good resource to understand the recommended way to install/update/uninstall entity types/fields in the update hooks.
Comment #5
Elijah LynnSlides showing differences from hook_update_n() and hook_post_update() from https://www.drupalgovcon.org/2020/program/sessions/using-post-update-scr... (recorded).
Comment #6
Pooja Ganjage CreditAttribution: Pooja Ganjage at Asentech LLC commentedHi,
From this below link, it shows difference between hook_update_N and hook_post_update functions.
https://events.drupal.org/vienna2017/sessions/update-hook-vs-post-update...
I hope it might help you out.
Thanks.
Comment #10
bkosborneGreat write up by Matt Glaman and some more information in the comments on it: https://mglaman.dev/blog/hookupdaten-or-hookpostupdatename
Comment #14
adriancidI think there is a clear explanation on this change record: hook_post_update_NAME() introduced to change content after hook_update_N() runs