Problem/Motivation
hook_install()
allows developers to do programatic things during a module or install profile installation. For example, the standard profile uses hook_install()
to create 2 shortcut content entities. This can be problematic because if the install is done during configuration import configuration entities are created after any module installs have been done. This is because it is possible to change a configuration entity and make it depend on things that are yet to be installed.
This problem has affected #2788777: Allow a site-specific profile to be installed from existing config and the Lightning install profile.
Proposed resolution
Add documentation to hook_install() to make developers aware of the problems of relying on configuration entities during hook_install() and ways in which modules have got around this.
Add an argument to hook_install $is_syncing
so that its front-and-foremost that hook_install
implementations need to consider this.
We can use this in forum.install in core as the test-case.
Remaining tasks
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#24 | 2914174-24.patch | 9.15 KB | larowlan |
#24 | interdiff-3f1574.txt | 620 bytes | larowlan |
#22 | 2914174-22.patch | 8.54 KB | larowlan |
#22 | interdiff-22a5b6.txt | 965 bytes | larowlan |
#20 | 2914174-20.patch | 8.58 KB | larowlan |
Comments
Comment #6
mxr576Bump!!! =]
I had the following problem:
@berdir Highlighted on Slack that I should run
if (!\Drupal::service('config.installer')->isSyncing()) {}
in install hooks every time before I try to modify a config. That fixed the problem.Comment #7
larowlanComment #8
larowlan@alexpott mentioned in slack that it might be worthwhile to pass an is_syncing argument to the hook so it was more obvious that you needed to make a decision based on that.
I agree with that decision, updating the IS.
Comment #9
larowlansomething like so
Comment #10
DanielVezaCould the param for this be
$is_syncing = FALSE
just to avoid changes being made to all existing hook_installs? Is that possible in this case?Comment #11
larowlanThis is just a sample implementation, and believe it or not, hooks don't have to add the argument if they don't want too (i.e. it will keep working without everyone having to change their implementations).
This is not dissimilar to hook_update_N if you want to use the
$sandbox
variable, you declare it as an argument, but if you don't it still keeps working. So I think we're good here - as you can see from the patch - I didn't change any of the other implementations, only forum module, but tests still passed.Comment #12
alexpott<3 this change. One look makes me wonder why didn't we get round to this earlier.
Terms are content :D - I know this has been copied from the forum example but that's a bit special.
Funnily enough the only core examples are all about not importing content on install. So perhaps we should broaden the docs to be something like...
Comment #13
larowlanSure
Comment #14
DanielVezaThat all looks good to me. Patch applies, standard profile and forum module all correctly enable. The install tasks for the forum module create the term as expected. All looks good to me.
I think we should change
core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
to use the new system as well, since that is using the service call. I couldn't find any more examples besides that in core.Happy to RTBC once that is done or if you have a reason why you don't want to do that in this patch. :)
Comment #15
alexpott+1 to changing the Umami install hook too. We'll also need a change record to tell developers about this. As it is not mandatory we don't have any BC implications but a CR is still useful.
Comment #16
larowlanComment #17
alexpottOops it's not demo_umami_install()...
It's demo_umami_content_install() and demo_umami_content_uninstall()... which actually brings about the excellent point that we should add this to hook_uninstall() too. Which in turn makes me ponder if this is something we should add to hook_modules_installed() and hook_modules_uninstalled().
Comment #18
alexpottAnd that triggered more memories... we have \Drupal::isConfigSyncing() too... so we need to update media_library_install() too. \Drupal::isConfigSyncing() is very prevalent in contrib.
Comment #19
alexpottI think adding to hook_modules_installed / uninstalled is a good idea.
Found at least one example that would benefit in contrib...
Comment #20
larowlanYep, don't rush stuff
Fixed and added the equivalent for uninstall, and modules_installed, modules_uninstalled
Comment #21
DanielVezaNitpick - Can this be one-lined? Also happy to be overridden on this.
Comment #22
larowlanSure
Added https://www.drupal.org/node/3098920 for change record
Comment #23
DanielVezaSo sorry - Just did a last review and there is still one more -
core/modules/media_library/media_library.install
with!\Drupal::isConfigSyncing()
in it.Comment #24
larowlannice!
Comment #25
DanielVezaPatch still applied, All config sync checks in install/uninstall hooks are changed.
Change record makes sense to me. Happy to mark as RTBC - Assuming the tests all come back green. :)
Comment #26
alexpottCommitted and pushed c72abf07aa to 9.0.x and c3c3bc8d4c to 8.9.x. Thanks!
I removed the fullstop on commit.