The documentation for implementing hook_load(), hook_delete(), hook_prepare(), hook_insert(), hook_validate(), hook_update() and hook_view() is unclear at best, incorrect at worst.

These modules are not implemented using the module name as the base name, but the node type's "base" instead. The documentation does not suggest that and the documentation that does explain it (in hook_node_info()) could be improved.

This patch seeks to correct the errors and improve the documentaion, including three textual examples of how one can use hook_node_info() and node type "base" to implement these hooks effectively.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Bevan’s picture

Status: Active » Needs review
jhodgdon’s picture

Status: Needs review » Needs work

Wow, good catch! This issue report is accurate -- if you look at function node_invoke(), which actually does the invokes, it is doing what you say.

However, I think the patch itself needs some work:

a) Proofreading -- such as spelling and "it's" instead of "its".

b) There are extra spaces (do not leave two spaces between sentences, only one).

c) I think it is a bit confusing. It could be pretty simple. Just say essentially:

When you define a node type, you give it a "base". All of the node-type-specific hooks (list them), instead of being named mymodule_hookname(), as they would be for normal hooks in module mymodule, are named base_hookname() instead.

I don't think the documentation needs to be this verbose.

Bevan’s picture

FileSize
6.84 KB

Thanks @jhodgdon! I tried really hard to make it as simple as possible, but I guess what I really needed was fresh eyes!

Also, I found that @defgroup node_api_hooks Node API Hooks attempts to cover this topic, but does so misleadingly too.

I like your phrasing:

  • It is not quite correct, because the base is required in hook_node_info(). I fixed that.
  • I removed personal pronouns.
  • I swapped the order of the phrases in the second sentence to prioritise important information and simplify overall.
  • I didn't list the hooks, since node_api_hooks group already lists all hooks, noting the node-type-specific ones.
  • I added the phrases to node_api_hooks group.
  • I used the keyword "node-type-specific" in the function header documentation for all node-type-specific hooks.
  • I added node-type-specific documentation to hook_form(), which was missing it, even though it is node-type-specific. (See $function = node_type_get_base($node) . '_form' in node_form()).
  • I referenced the new, correct and improved documentation with @node_api_hooks in hook_node_info() and all node-type-specific hooks.
  • I mentioned node_content as a common option for this since it is the base for content types created via the UI and Features-managed content types, and it allows a content module to provide it's content type without needing to implementat hook_form(). See node_content_form(): It is impossible to define a content type without implementing hook_form().

Also; I am not sure how to set a new paragraph inside a list item? Or if that is even possible via doxygen, and how to test it. Do you know?

Bevan’s picture

Status: Needs work » Needs review
jhodgdon’s picture

Status: Needs review » Needs work

This is great!

A few minor things to fix:

a) Lists in API documentation do not support paragraph breaks within a single list item. So you should just wrap the text together, in:

...
+ *   'mymodule' for example). Only the node type's corresponding implementation
+ *   is invoked.
+ *   For example, poll_node_info() in poll.module defines the base for the
...

b) You cannot use @see inside of documentation paragraphs. @see goes at the end. Use @link to make links within text. So:

+ *     hooks that respond to this node type. Base is usually the name of the
+ *     module or 'node_content', but not always.
+ *     @see node_api_hooks

should become something like

+ *     hooks that respond to this node type. Base is usually the name of the
+ *     module or 'node_content', but not always. See
+ *     @link node_api_hooks Node API hooks @endlink for more information.

And

+ * This node-type-specific hook is invoked only for specified node types.
+ * @see node_api_hooks

needs a similar change.... but maybe on each hook we should say something like:

This is a node-type-specific hook, which is invoked only for the node type being affected. Also, instead of the function name prefix being the module name, it is the "base" of the node type. See @link node_api_hooks Node API hooks @endlink for more information.

It seems like it is pretty important here to know that the function name is not like other hooks?

er.pushpinderrana’s picture

Status: Needs work » Needs review
FileSize
7.47 KB

Incorporated above mentioned changes in this patch. Please review updated patch.

jhodgdon’s picture

Status: Needs review » Needs work

Looks great!

I did notice one thing I think we should fix, in hook_form():

+ *
+ * Use hook_form_alter() to respond to node forms for all node types.

We should not advise using hook_form_alter(). People would be better of using hook_form_BASE_FORM_ID() alter, since it is more specific. So we should say something like:

Use hook_form_BASE_FORM_ID_alter(), with base form ID 'node_form', to alter node forms for all node types.

The rest looks perfect, thanks!

er.pushpinderrana’s picture

Status: Needs work » Needs review
FileSize
657 bytes
7.52 KB

Please review updated patch with #7 changes.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the quick update!

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Thanks again! Committed to 7.x.

  • jhodgdon committed b15cfbb on
    Issue #2309549 by Bevan, er.pushpinderrana: Fix incorrect documentation...
Bevan’s picture

Thanks @er.pushpinderrana!

Status: Fixed » Needs work

The last submitted patch, 8: 2309549-8.patch, failed testing.

jhodgdon’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

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