0

I want to create dynamic options based on the entity types (returned by entity_get_info() function) inside the hook_drush_command(). There's no bootstrapped Drupal available at all, so the db_query() entity_get_info() functions aren't available. How can hook_drush_command() get site information?

(The module is inside the ~/.drush/mymodule directory.)

  • You can't (neither can Drush itself). You need to execute Drush under an active site, then you'll be able to use Drupal's API – Clive May 8 at 14:26
  • The problem is, even if I execute the help command when I'm under an active site, I can't access to the site db inside the hook_drush_command() function. – golddragon007 May 8 at 14:34
  • That makes sense - that hook is for registering Drush commands, so it needs to be able to work whether there's an active site or not. Basically you can't do it the way you want to, it wouldn't conceptually work. The callback/parameters for the custom command will have to provide the functionality – Clive May 8 at 14:37
  • Ok, than how can I say to the user, what the user can use, when it variate from site to site, what entities are defined? – golddragon007 May 8 at 14:43
  • And it's also interesting, because until it was a Drupal module, it worked and it was accessible the entity_get_info() function from hook_drush_command()... – golddragon007 May 8 at 14:52
1

This should work. Drush bootstraps Drupal before discovering module commands, so the first step is to make sure your commands are defined in a drush.services.yml file as usual.

From there, you should be able to define dynamic options using hook options. Since Drush bootstraps Drupal prior to discovering module commands, you should find that the Drupal site is always bootstrapped when your hook is called. You can inject the services you need through your drush.services.yml file.

EDIT When I read through the comments I thought that you said this was working when you were using hook_drush_command, so I answered as if you were asking how to convert and do the same thing in Drush 9. I don't remember the internals of the convoluted Drush 8 bootstrap very well, but I'm not sure this is possible in Drush 8.

  • It's working if I place my commands under Drupal direcotry as a Drupal module. But I wanted to be a global module, and in that case I need to put into the ~/.drush/ place, where it doesn't work anymore... – golddragon007 May 10 at 7:00
  • Did you try setting 'bootstrap' => DRUSH_BOOTSTRAP_FULL? Check other commands for exact syntax. – greg_1_anderson May 15 at 21:46
  • Yes, I set, but it only applies to the callback of the command, and not for the hook_drush_command() function. I also tried to bootstrap the drupal inside the hook_drush_command() if it's fount, but I didn't fount a way to do it. – golddragon007 May 16 at 9:09
  • Try writing an extension for Drush 9 following the example of github.com/drush-ops/example-drush-extension and my answer above. I don't think it's likely that you're going to be able to make this work in Drush 8. – greg_1_anderson May 18 at 23:20
  • Drush 9 does not support Drupal 7, so for this for me is useless. – golddragon007 May 20 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.