This is as much about documentation as tests, but putting here.
At the moment, it's a real pain to do documentation for hooks having to update them in CVS (I've never done this, but that's confirmation that it's a pain) - additionally, we've got an increasing number of test modules in modules/simpletest/tests - some of which implement hooks.
So here's a proposal - we create modules/simpletest/tests/hook.module - this module implements as many hooks as possible, and also provides a target for testing if there's not an appropriate target elsewhere - this gives us a few things:
1. searching for hook_foo on api.drupal.org will return documentation for hook_user instead of nothing.
2. If I add a hook, or want to change the documentation for a hook, I can patch core instead of messing with the big hooks table in CVS
3. These patches will be recognised properly by the testing bot
4. We'll have code examples of hook implementations in core which a lot of people have looked over - so we can grep core as well as/rather than contrib
5. We have a single module to patch against for miscellaneous hooks, as opposed to having to create arbitrary new modules in the simpletest directory - which could get very messy very soon.
and etc.
There's some potential for confusion here - but I think if we handled it properly, the advantages would outweight this.
Comment | File | Size | Author |
---|---|---|---|
#43 | backout_330633_mixed_in_with_314870.patch | 4.53 KB | drewish |
#39 | node_314870.patch | 136.86 KB | drewish |
#36 | api_314870.patch | 132.89 KB | drewish |
#30 | 314870.patch | 132.99 KB | RobLoach |
#29 | 314870.patch | 132.99 KB | RobLoach |
Comments
Comment #1
RobLoachI like the idea of moving stuff in http://cvs.drupal.org/viewvc.py/drupal/contributions/docs/developer/ to core. Having documentation available in core is very handy, and definitely would help when visiting api.drupal.org is not an option.
Comment #2
drewish CreditAttribution: drewish commentedThe thing I like about it is that we can ensure that the documentation for hooks gets reviewed and committed and at the same as the code changes.
Comment #3
drewish CreditAttribution: drewish commentedWait... actually combining the two would have one big downside. The code required to check that a hook's been called for unittesting is totally not the sort of code that you'd want to show up as an example of implementing the hook.
Comment #4
catchHmm. That's a good point. Having said that, there's no reason we couldn't separate the two. We can call the simpltest specific one hook_test.module and that'd be no problem at all.
Comment #5
RobLoachThis patch takes the hook documentation found here and sticks it in a hidden module. Not too sure where to go with the test system though. Visiting admin/build/modules doesn't display the Hook module.
Comment #6
sunA certain amount of contrib modules is also implementing hooks. Instead of a hidden module in core, I'd rather like to see a unified way for including developer documentation compatible to API module. What about creating a sub-folder /hooks in each module (system for other core hooks) that contains one or multiple files?
Comment #7
catchIn irc, Crell suggested a module.hooks.inc - which we never actually load because module.info wouldn't specify it - and so the registry would never read it. This would allow things to show up on api.drupal.org, but I think it could be very bad DX to have a .inc that would never be loaded along with all the others that are, so I'm not convinced.
RobLoach: amaaazing, no time to look right now though, but will check it out soon.
Comment #8
RobLoachhttp://drupal.org/node/224333#remove_op
Comment #9
webchickLet me preface this comment by saying +10,000 for having hook documentation in core. Moving the database schema documentation into core was one of the best things we ever did and has greatly helped to ensure that fields are a) documented at all, b) continue to be updated throughout the ages.
However, I don't like this exact approach.
a) While catch's #5 is a good point, I would prefer to find a solution that could work equally well for both core and contrib. Each module defining its own modulename.hooks.php file, or similar? Basically, see sun's #6.
b) However, the question then comes up of how to deal with hooks exposed by core include files (menu, file, etc.)? Blah.
c) I'm a little leery of using module.hook.inc, because module.*.inc is generally used for page-splitting patches and people might get confused that that's where you need to put your "implementation of X" in there. I'd prefer a clearly different naming convention altogether.
d) I prefer precision tests, such as the file and db tests, over a gigantic hook.test trying to test every nuance of every hook. Maybe I misunderstood that point.
Comment #10
catchWe could have a hook.hook_name/group.test for each hook - but have them all attached to hook.module Would keep things granular. The main things with hook.module is it never gets loaded anywhere except by simpletest - I'm not sure how we'd handle that with the .incs.
Comment #11
moshe weitzman CreditAttribution: moshe weitzman commentedWhat about having each module ship with an api.php in its directory. Its only purpose is for api.module to scan it. That's more modular than a single hooks module and applies equally well for contrib.
Comment #12
webchickI like that idea. But will API module choke on multiple files named the same thing?
Comment #13
webchickI guess if it will, we could always just fix API module. ;)
Comment #14
catchapi.php sounds good, and is much more self documenting than module.hook.inc which was always going to confuse people.
Comment #15
webchickOk. Let's do it!
api.php in each module directory. Misc. crap in the includes directory gets put into modules/system/api.php.
GO! :)
Comment #16
catchComment #17
RobLoachIn some contrib modules (like Views), there are multiple modules that live in the same directory. Does this mean that the api.php would include the API for all the modules in that directory? If we used modulename.api.php, then the hooks for each module in that directory could be defined all in separate files.
Will SimpleTest call these hooks during tests?
Comment #18
moshe weitzman CreditAttribution: moshe weitzman commentedIIRC you can't have multiple modules in same directory after the registry patch. I might be remembering wrong. i'm fine with modulename.api.php (or .inc?) if needed
Comment #19
webchickThat Views question is interesting. Especially when you consider something like Ubercart with 50,000 sub-modules or so.
I'm starting to now lean in the other direction: A single 'hooks.php' in the root of the 'project' with /** @defgroups **/ as makes sense, with ALL of the hooks defined by that project, regardless of where they live. That's better from a DX perspective as I don't need to go traipsing through directories to get a big picture of what a thing does. It also makes this patch much easier to roll. :)
So in core, we could have @defgroup Core, Node, etc.
Views might have @defgroup View styles, View plugins, View yo momma
Does that make sense?
Comment #20
dman CreditAttribution: dman commentedSurely
modulename.api.php
is more consistent withmodulename.module, modulename.info, modulename.install
Comment #21
webchickSure, I'm not too fussed about the name (other than it should not be .inc, because it's never being included anywhere). I'm more interested in whether the approach is sound.
Comment #22
pwolanin CreditAttribution: pwolanin commentedif I were developing many sub-modules, I'd want the ?.api.php file to be grouped with each module - anything else would seem like a nightmare to track and fix.
Comment #23
moshe weitzman CreditAttribution: moshe weitzman commentedYes, one api.module in each module subdirectory. If you aren't dividing your modules into subdirectories in a project, you are not following best practices and we need not specify an api.php convention for this.
Comment #24
sunI am right now in the situation of having to document Wysiwyg API. My considerations and reasoning:
First, I placed a file api.php in the module folder.
That looked at bit lost, because all other files in the folder are prefixed with the name of a module.
Renamed to wysiwyg.api.php.
Makes more sense and is more consistent now.
Also, I can see modules like Views or eCommerce that provide so many API hooks that a single file would be cumbersome.
Sure, the best practice is to have sub-modules in sub-folders.
But then again, look at Views and Views UI, both being in the module's root folder. Both may or may not provide API hooks.
Usually, core avoids too strict rules where they are not required.
If we simply define a standard like module.api.php, systems like API module or SimpleTest can search for a certain filename pattern, regardless of the file's location.
So if we would want to place all core API documentation in one folder, we could do so; i.e. node.api.php, user.api.php, etc.
The same for contrib modules.
However, we could also place node.api.php in modules/node/ - what matters is just the filename.
Hence, +1 for <module>.api.php
In the end I think that drumm should have the final word on this.
Comment #25
drewish CreditAttribution: drewish commentedi'm in agreement with sun's reasoning in #24.
Comment #26
RobLoachWhere will the core hooks go that are not associated with a module? In modules/system/system.api.php? includes/api.php?
Comment #27
RobLoachI went through the hook documentation and attempted to move all the hooks to their designated module.api.php. I also placed a includes/api.php for hooks that are not quite associated to any module. Some of api.php might belong in system.api.php, however.
Comment #28
RobLoachAs discussed on IRC with webchick, tha_sun and add1sun, we decided to move all of includes/api.php to modules/system/system.api.php.
Comment #29
RobLoachForgot the patch....
Comment #30
RobLoachSome of the descriptions were incorrect....
Comment #31
sunWe can simplify this to "Hooks provided by Xyz module.":
Probably all of those are not required and can be removed:
hook_hook_info() in system.api.php belongs into trigger.api.php, I guess.
hook_update_index() in system.api.php belongs into search.api.php, I guess.
There are 2 @file definitions in node.api.php.
EDIT: Oh, and did I say: AWESOME! ? ;)
Comment #32
catchhook_hook_info() - isn't this provided by core actions, which are independent of trigger?
hook_update_index() should be search though yeah.
Comment #33
webchickI'd like to get this in in UNSTABLE-4. It's starting to get annoying tracking all the various follow-up issues and request for documentation after the fact for new/modified hooks.
Comment #34
dmitrig01 CreditAttribution: dmitrig01 commentedalso nodeapi is now out of date
Comment #35
dmitrig01 CreditAttribution: dmitrig01 commentedsorry for title change
Comment #36
drewish CreditAttribution: drewish commentedUpdated this a bit:
Didn't mess with the hook_nodeapi docs at this point.
Comment #37
catchDifferent bits of hook_nodeapi are in line for slicing by at least three patches at the moment - so let's fix that once it's in core so as not to hold this up - will have to patch them for the forthcoming API changes anyway, at worst we fix it at the same time then. And this saves docs getting out of date ever again.
Patch applies and passes tests, so I'm going to RTBC this.
Comment #38
webchickthat extra hunk should be taken out... was a remnant of when node hooks were in hooks/node.php instead.
Comment #39
drewish CreditAttribution: drewish commentedhere's a re-roll without the wierd @file chunk in the middle of node.api.php.
Comment #40
RobLoachThanks a lot, Drewish. Looks great! RTBC * 2.
Comment #41
webchickAwesome. Apart from some desperately needed clean-up we have left in some of this documentation, this looks really great, and will ensure that all future hook changes and additions aren't committed until they update the API documentation. It will also help people frequently on planes who don't keep a checkout of contributions/docs/developer/hooks with them.
Committed to HEAD. Thanks! :D
We need some upgrade documentation, please.
Comment #42
Dave ReidI don't understand why there are changes to system.test and system.module in this patch that was committed... It is related to temporary file cleanup and not related to apis at all.
Comment #43
drewish CreditAttribution: drewish commentedThat was my bad... I'd accidentally rolled #330633: Temporary file cleanup needs some love (UnitTest included!) in.
Here's a reversal of http://cdn1.drupal.org/files/issues/file_330633_2.patch which should do the trick.
Comment #44
RobLoachYup! Dave Reid has ninja eyes that are better then both webchick's crazy eagle eyes and my lack-of-coffee eyes.
Comment #45
Damien Tournoud CreditAttribution: Damien Tournoud commentedBumped to critical: the code that sneaked in broke PostgreSQL installation with message:
Comment #46
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.
Comment #47
RobLoachAdded update documentation.
Comment #48
Dave ReidShould we delete all the files in the contributions/docs/developer/hooks/ now?
Comment #49
drewish CreditAttribution: drewish commentedI think it'd be a good idea since api.drupal.org is using the new .api.php files: http://api.drupal.org/api/group/node_access/7
Comment #50
Dave ReidI removed all the files from the directory. If we notice missing documentation once api.drupal.org regenerates, we'll have to file separate issue to put it in our new core api.php files!