In response to pwolanin's post on the devel mailing list about node types, we should introduce a new hook_node_type(), which will allow modules to react to node types being created / updated / deleted. This hook would work very much like hook_user() and other similar hooks.

CommentFileSizeAuthor
#4 hook_node_type.patch4.93 KBJaza
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pwolanin’s picture

For this hook to be reliable invoked, it would seem that any node type creation/deletion needs to be done through a function call rather than through any direct insert into the DB. The system.install file seems to do it two different ways for install vs. update. However, what I find a little odd is that the function node_type_save() is not used in either, and also that this function doesn't include the calls below (from system.install), in a fashion similar to node_save() calling cache_clear_all() at the end.

  cache_clear_all();
  system_modules();
  menu_rebuild();
  node_types_rebuild();
KarenS’s picture

+1 on the idea of creating this hook.

See an issue at http://drupal.org/project/comments/add/90497 that shows one place the hook would be useful - to keep the vocabulary_node_types table up to date when content types are deleted. Right now the node module has no hook to allow the taxonomy module to act, and the taxonomy module has no hook that would allow the node module to act. It makes some sense to me for the node module to provide a hook when content types change and for the taxonomy module to take responsibility for keeping its own table up to date by using that hook.

pwolanin’s picture

Category: task » bug
Priority: Normal » Critical

marking as critical- 5.x shouldn't go to RC without a fix for this problem. Either we need a well-implemented hook of the type suggested, or the ability to change to type names needs to be disabled (per http://drupal.org/node/85062). I'm trying to update one of my modules (node_clone) for 5.x now, and want to help update the event module. Both of these (as well as the core taxonomy module) save settings based on the node type. If the type name changes, all the setting are lost and the site breaks.

Jaza’s picture

Assigned: Unassigned » Jaza
Status: Active » Needs review
FileSize
4.93 KB

Attached patch does the following:

  1. Invokes the new hook_node_type() in the node_type_save() function, for insert and update of node types.
  2. Implements the hook in taxonomy.module, for updating content type associations of vocabularies.
  3. Implements the hook in node.module (in content_types.inc), for updating the content type of posts (this is currently done directly in node_type_form_submit() - the new approach is much better, as it allows for node types to be saved programatically, without submitting a form, and for these actions to still take place when the hook fires).

The hook needs to be invoked and implemented for 'delete' as well, but this belongs in a separate patch - see http://drupal.org/node/83202 - it will go here. This patch is ready for review, and is hopefully still able to go in for Drupal 5.

KarenS’s picture

This looks good to me except that I don't see how this will answer my original problem -- how do you get the taxonomy module to remove a deleted node type from the vocabulary_node_types table? Your hook won't do it and your separate patch won't do it either.

I definitely would like to see this go into 5.0 though. It will be needed by CCK, too, since CCK is managing tables and caches that need to be updated when content types change. In 4.7 CCK can tell when content type changes happen and react to them, in 5.0 it cannot.

KarenS’s picture

I closed the issue about the taxonomy not updating the vocabulary_node_types table at http://drupal.org/node/90497 and redirected the discussion over here, since this patch is the best way to solve that problem (at least it will be if some provision for handling content type deletions is provided).

drumm’s picture

Status: Needs review » Fixed

Committed to HEAD, with a slight modification to the node.module changes for simpler code.

Jaza’s picture

Updated the developer docs (that link isn't working yet, but it should work soon) and the module upgrade docs.

AmrMostafa’s picture

Status: Fixed » Needs work

Sorry, but the 'Changed association...' message isn't t()-ed.

Actually, I think this message should be removed, users already expect things to work together :-).
And if it's not, then we will also need to print a similiar message in similiar cases, like http://drupal.org/node/85062. the messages list can be long this way.

AmrMostafa’s picture

Status: Needs work » Fixed

apologies, completely forgot about format_plural. I still think the message should be removed though :-).

Anonymous’s picture

Status: Fixed » Closed (fixed)