There has been much talk that activate/deactivate hooks on modules need to wait for the future.

I decided to ask myself why and I looked at system_modules and noted that a patch to allow these hooks would be completely trivial.

To with, this patch is attached. I hope to get commentary from the likes of Dries and Steven -- I think this is a great idea, and it's something that some modules really need.

Comments

merlinofchaos’s picture

StatusFileSize
new1.08 KB

Updated patch: Moved module_invoke($module, 'activate') above comment (which was for next line).

colorado’s picture

NewbieQ - What does this mean, "activate/deactivate hooks"?

merlinofchaos’s picture

Specifically, when a module's status changes from 'active' to 'inactive' or 'inactive' to 'active' at admin/modules, the module will get hook_deactivate() or hook_activate() called so that it can do cleanup/setup/what have you.

chx’s picture

Status: Needs review » Reviewed & tested by the community

This is a very simple, very non-obtrusive but extermely powerful feature.

drumm’s picture

I think we should stick with the enabled/disabled terms.

fool2’s picture

If we are to implement this I think it should be a little more complicated.

What if someone just wants to temporarily disable a module? If Database installation and deletion is in the these hooks, then you can't do that because you'll lose the data.

merlinofchaos’s picture

I think anything that complex is a feature for 4.8; this is simple and will work.

A module would be foolish to do permanent data deletion in deactivate. deactivate shouldn't be considered the same as uninstall. On the other hand, there are things some modules do (especially if they are node_access using modules) that do need to make some kinds of database changes when the module is deactivated).

merlinofchaos’s picture

StatusFileSize
new1.07 KB

alternative patch using enable instead of activate attached.

jjeff’s picture

+1

oh and also...

+1!

drewish’s picture

+1 I like the enable/disable names better than activate/deactivate.

Also, the install hook has been committed to HEAD but it's not documented yet. The docs for this need to make it clear that db schema setup belongs there.

drewish’s picture

Personally, I don't think new hooks should be committed without documentation. Here are some very basic docs for contrib/docs/developer/hooks:

/**
 * Perform setup tasks when the module is enabled.
 *
 * This hook is run when the administrator enables the module.
 *
 * Note: Database table creation should be done in hook_install(), not here.
 */
function hook_enable() {}

/**
 * Perform cleanup tasks when the module is disabled.
 *
 * This hook is run when the administrator disables the module. 
 */
function hook_disable() {}

Comments and criticism are welcome. An example would be nice... someone is testing this with an access module... right?

I'll roll this into a patch and open a documentation issue once this is committed.

keve’s picture

+1 for hook_disable

I am developer of Taxonomy Access Controll.
From time to time i get request for this from users.
(Since they forget to disable TAC on settings page, afterwards they have much trouble w/ accessing/editing nodes, because of the remaining values in table 'node_access')

merlinofchaos’s picture

StatusFileSize
new1.05 KB

Patch cleaned up slightly based upon Steven's comments via IRC.

keve’s picture

How are these hooks standing? Can it be commited soon?

drewish’s picture

Version: 4.7.0-beta3 » 4.7.0-beta5

i'd love to see this comitted before 4.7...

keve’s picture

I'd love it,too.

Although with taxonomy_access, i got around the problem with hook_form_alter.
On page /admin/modules TAC cannot be disabled until deactivated on it settings page.

Steven’s picture

Status: Reviewed & tested by the community » Needs work

Unless I'm reading this patch wrong, hook_enable would be invoked before hook_install. That does not make sense.

Crell’s picture

Version: 4.7.0-beta5 » x.y.z

Whoever's fixing this patch next, it belongs to CVS now.

asimmonds’s picture

Status: Needs work » Fixed

Module enable/disable hooks went into HEAD recently with patch #76209 [1]

[1] http://drupal.org/node/76209

Anonymous’s picture

Status: Fixed » Closed (fixed)