From Stack Exchange:

http://drupal.stackexchange.com/questions/79178/drush-post-command-hook-...

It is kind of unfortunate that all of Drush's nice command hooks just go away if the module maintainer does not follow proper conventions (i.e., only use 'callback' if absolutely necessary). It would be possible to call the hooks if we tried harder.

Comments

greg.1.anderson’s picture

Status: Active » Needs review
StatusFileSize
new4.61 KB

Here's a patch. The code was practically made to do this. It's much less confusing for folks if they can count on hooks firing uniformly. (Note: the two new calls to drush_log are nice for testing this, but perhaps should be removed before committing it to reduce noise in debug mode. Then again, most commands only implement one callback hook; maybe it's okay to keep these here.)

wonder95’s picture

First of all, thanks for this patch and for whipping it up so quickly.

I tested it, and was able to use my post_COMMAND() hook with the callback still defined in the command.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Looks fine to me. Ideally we add a test but we can @todo that if needed.

greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

This was really easy to test; all I needed to do was add a callback to the unit-invoke test. Without this patch, hooks are not supported, and the test fails.

Adding this test exposed another issue. Prior to this patch, the command hook was based on the callback name, not the command name, in instances where the callback hooks were supported. This isn't right in the general case, though, where the command hooks are supported regardless of the naming conventions used by the callback function. For example, in the case of unit-invoke, when I added a callback that changed the primary function to drush_unit_invoke_primary, that changed the command hook from unit_invoke to unit_invoke_primary. I therefore changed the code in command.inc to preserve the command hook, so that the addition of a callback function would not change the name of the command hook functions.

This is a change from Drush 5, and will affect any command that depends on this somewhat odd behavior. The only thing I found so far that does depend on this is the completion unit tests. The easy thing to do in instances like this is to set the command hook in the command record. The command hook is what is used to generate the function names used in the command hooks, so setting the command hook for all of the completion commands to the same value restored the previous behavior it depended on.

For details, see http://drupalcode.org/project/drush.git/commitdiff/f3963f0

moshe weitzman’s picture

Assigned: Unassigned » greg.1.anderson
Priority: Normal » Critical
Status: Fixed » Active
Issue tags: +Release blocker

I think this introduced a test failure in testCacheGetSetClear class. I am seeing two command callbacks are getting called instead of one. drush_cache_command_get (good) and drush_cache_get (bad). Let me know how you think this should be fixed.

greg.1.anderson’s picture

Status: Active » Needs review
StatusFileSize
new2.85 KB

I made two adjustments to fix this. First, the 'normal' command hook is no longer called if a 'callback' command hook is supplied. This should be sufficient to fix the reported problem. Second, I added an 'invoke hooks' item that may be set FALSE to avoid all command hooks when a 'callback' is specified. This can be used if a function is still experiencing filename collisions with the command hooks.

moshe weitzman’s picture

Priority: Critical » Normal
Status: Needs review » Fixed

Committed, after adjusting commandCase test so it passes. Thanks Greg.

Note that I'm not sure we should have made another configuration element for the completion hooks. I'm thinking we should have renamed those functions and expected anyone else using the old behavior to do the same. Not a big deal though.

wonder95’s picture

One incredibly minor nit to pick on the last patch: in the doc block, the correct spelling is "suppress" (not "supress)".

greg.1.anderson’s picture

Fixed typo. Thanks.

Automatically closed -- issue fixed for 2 weeks with no activity.

eastdrive’s picture

Issue summary: View changes

I'm trying to apply this patch but I can't find a version of Drush that it's compatible with - i.e. that contains an implementation of _drush_invoke_hooks().

Can anybody help me?

Many thanks.

helmo’s picture

@eastdrive: This already got committed per #7 and is in the 6.0 version.
See http://drupalcode.org/project/drush.git/commitdiff/a0e602c1236e8aefe079a...

eastdrive’s picture

Thanks very much @helmo. My hooks still aren't firing so it must be something we're doing wrong.

Your input was very much appreciated.