I need to run a hook after a drush command in another module has been run to clean up a file on the server, and so based on what I've read here and here I'm trying to implement a post command hook on the node-export-import command. In mymodule.drush.inc I have a function defined as

function drush_mymodule_post_node_export_import(){
  // Do my magic here

but it isn't called at all after running the node-export-import command. I've played around with other naming variations, but none are getting called. My module is enabled, and I've cleared cache. I'm using version 5.9. Is there something I'm missing?



To see all of the hooks available to you, run your command with --show-invoke, like this:

$ drush status --show-invoke 2>&1 | grep mymodule

However, when we do this on node_export commands, we see no results. Why is that? Well, unfortunately, some commands do not go through the drush invoke hook process. If you look at the definition of the node-export-import command, you will see this:

$items['node-export-import'] = array(
  'callback' => 'drush_node_export_callback_import',

In Drush's command.inc, there is this comment in the function drush_get_commands():

    // If command callback function name begins with "drush_$commandfile_",
    // then fix up the command entry so that drush_invoke will be
    // called by way of drush_command.  This will cause all
    // of the applicable hook functions to be called for the
    // command when it is invoked.  If the callback function does
    // -not- begin with its commandfile name, then it will be
    // called directly by drush_dispatch, and no hook functions
    // will be called (e.g. you cannot hook drush_print_file).

If a command provides a callback, then Drush calls that function instead of drush_command, so _drush_invoke_hooks is never called. This means that, even though you named your hook function correctly, Drush will not call it, and you are stuck.

From here, you have two options:

  1. Post an issue to the node_export issue queue, and request that they stop using the 'command' callback parameter, and let Drush choose the function name to call. This will make hooks work again. Using 'callback' is deprecated unless it is necessary (e.g. many commands calling the same function, such as drush_print_file), so hopefully the maintainers will be willing to do this.

  2. Post a feature request to the drush issue queue, and request that Drush call the invoke hooks even if the Drush command uses the callback feature. This is theoretically possible if the parameters to _drush_inovke_hooks were enhanced to include a replacement function name to call in place of the primary hook function. This would have to be set up in drush_get_commands(), and then detected and uses in drush_command() where it calls _drush_invoke_hooks. This didn't seem like a good idea at the time, but if it is common for contrib module maintainers to ignore subtle Drush conventions, it might be worthwhile to go to the extra effort to let folks hook their functions anyway.

Update: I made a patch for you. See: https://drupal.org/node/2041109. You might still want to do #1, as I don't think that we will necessarily backport this enhancement to Drush 5.

Update 2: Above-referenced patch has been committed to Drush-8.x-6.x-dev. This happened after the rc1 release, but it will be in the next release.

Update 3: This is now available in Drush-8.x-6.0-rc2.

  • 2
    Wow, thanks for the detailed answer and patch. Considering the level of support for node_export, I'm not hopeful that the patch would ever be committed, so I upgraded to the current 6.x version of drush, applied the patch, and it worked perfectly. – wonder95 Jul 12 '13 at 22:44

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.