3

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()?

1 Answer 1

3

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

4
  • 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. Commented Mar 29, 2022 at 14:31
  • 1
    I resolved with batch_set Commented Mar 29, 2022 at 14:45
  • Can you post a full example of your use case? Commented Jun 22, 2022 at 19:50
  • 1
    @HodbaKhalaf – You mean the full example that's linked in the last line of my post? drupal.stackexchange.com/a/310282/15055 Commented Jun 22, 2022 at 20:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.