0

Here is my use case.

I'm importing nodes with the classic migration API and goes through some hooks (like hook_node_presave, hook_node_delete...).

I also have another drush command that synchronise content and does some other stuff and goes through those same hooks.

I want to do some specific things in those hooks wether it's called by a back-office action or drush migrate:import or drush command1. Is there a way to pass a context of some sort on the drush command that would be retrievable in my hooks ? Or at least to know the hook is executed by a back-office action ?

I looked there and there but it was not satisfying. Knowing it's executed by PHP_CLI is not precise enough for me in my use case.

7
  • Does this answer your question? stackoverflow.com/questions/1991346/…
    – mona lisa
    Commented Jun 19 at 10:31
  • Thanks for your proposal, I'm not really fan of this solution :-)
    – pbonnefoi
    Commented Jun 19 at 12:56
  • The question title is "Is there a way to know that I'm executing a drush command on the code side?"
    – mona lisa
    Commented Jun 19 at 13:14
  • Indeed, but the proposal in the link is not precise enough. I want to know which drush command is executed.
    – pbonnefoi
    Commented Jun 19 at 14:44
  • 1
    The question needs an edit in that case.
    – mona lisa
    Commented Jun 19 at 16:49

2 Answers 2

5

You should be able to get all information from Drush::input() and Drush::getApplication(), but first check if you are running from command line and Drush is installed:

if (PHP_SAPI === 'cli' && class_exists('\Drush\Drush')) {
  $command_name = \Drush\Drush::input()->getFirstArgument();
  $full_command_name = \Drush\Drush::getApplication()->find($command_name)->getName();
}
0
2

This isn't exactly an answer to your question, but is there a reason you must do those specific things in those hooks as opposed to using the Migrate API Events and Hooks.

It seems those could work and have the context they need about when they are firing.

1
  • Thank you for your proposal. In case of migrate command, yes that could be a solution, but I also have another context were I execute my own custom drush command.
    – pbonnefoi
    Commented Jun 20 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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