Update hooks don't always have to do something technical. They can also be used to inform administrators / site owners running the update about important or even breaking changes (BC).
For example this might happen if manual interaction is required for:
- Breaking template change
- New permissions added
- ...
see paragraphs.install for example:
function paragraphs_update_8013() {
// [...]
return t('Paragraphs can now display unpublished Paragraphs to users with the "View unpublished paragraphs" permission. Enable it on Adminstration > Configuration > Content > Paragraphs and grant to permission to use it.');
}
So just create a Drupal update hook (hook_update) and return the text to inform the user.
Advanced: Inform the user about possible actions conditionally
Of course, the messages can also be conditional. For example, to check is a removed dependency can be uninstalled:
(Example from drowl_media.install):
/**
* Add information to uninstall media_entity_generic now.
*/
function drowl_media_update_8302(&$sandbox) {
if (\Drupal::moduleHandler()->moduleExists('media_entity_generic')) {
return t('You may now want to UNINSTALL AND REMOVE "media_entity_generic" module, if it is not used by other modules.');
} else {
return t('The "media_entity_generic" module is not installed, everything is fine already!');
}
}
Advanced: Let update fail, if a precondition is not met
Update hooks can even be used to ensure certain preconditions are met, for example ensure a newly added dependency is existent, which means the module is enabled.
But of course, all kinds of checks / conditions are possible!
(Example from drowl_layouts.install):
/**
* Ensure twig_real_content module is enabled (new dependency).
*/
function drowl_layouts_update_8401(&$sandbox) {
if (!\Drupal::service('module_handler')->moduleExists('twig_real_content')) {
throw new \Drupal\Core\Utility\UpdateException('twig_real_content module is required as new dependency. Please download and install twig_real_content and re-run the update afterwards.');
} else {
return t('New dependency module twig_real_content is already enabled. Fine!');
}
}
Note: In most cases this should be done using hook_requirements!