Score:2

How can I execute a batch process in hook_install()?

in flag

When my module is installed, I need to execute a batch process, loading hundreds of entities and doing stuff with them.

This is trivial with hook_post_update_NAME() and its $sandbox variable. How can I achieve the same sort of thing via hook_install()?

Score:2
ne flag

If it's a custom module you can use hook_deploy_NAME(&$sandbox) inside MY_MODULE.deploy.php. drush deploy:hook being executed as part of drush deploy will pick up any hook_deploy_NAME implementation even of just recently enabled modules.

So make sure you execute drush deploy as part of your deployment routine, then your custom module will get enabled first from drush cim (and an entry in the core.extension.yml of course) and your custom install logic will get run from drush deploy:hook being executed after it.

Found out just recently that hook_deploy_NAME implementations won't get registered on module enablement like hook_update_N implementations are. Every existing hook_deploy_NAME implementation will just be picked up.

Talked about it quickly with Moshe Weitzman (the Drush maintainer) on Drupal Slack asking if this is a bug and he said no it's a feature and that hook_deploy_NAME implementations are supposed to only be used in custom modules.

See https://drupal.stackexchange.com/a/254411/15055 for all the commands that are part of drush deploy.


Have you tried batch_set in the install hook? https://drupal.stackexchange.com/a/310282/15055

sonfd avatar
in flag
Oh, I totally forgot about `hook_deploy_NAME()`. Unfortunately, this site is not using `drush deploy` yet. And for some reason I didn't think you could use `batch_set` in `hook_install()`, but you totally can.
sonfd avatar
in flag
I resolved with `batch_set`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.