2

I have made a new content type in Drupal 7 in Drupal Admin (not via module). What I want to do is to add a Hook on the insert and update operation. How can this be done when my node type is not created by a module?

Can this be done at all?

  • Is there a reason for not writing your own module for this? – nonsenz May 16 '11 at 11:33
  • I like to create node types in Drupal through the admin interface rather than using modules. This is done so that making changes is easier, and even maybe let the customer do necessary changes. Of course, I am starting to think this is not possible, so I guess I'll be starting on a module shortly. – pusle May 16 '11 at 12:09
4

You want to create a custom module. Note that nothing stops you from creating content types through the user interface, it is in fact the suggested way to do so nowadays with fields and stuff (instead of using hook_node_info())

Node API Hooks contains an overview of all hooks that can be used to interact with nodes at different events like create, view, delete..

  • If I understand correctly, I can create a node type through the user interface, and create a module that hooks into node operations on this node type? – pusle May 16 '11 at 13:24
  • Yes. Only the node type callbacks (hook_form, hook_insert, ..) are only called for the module that defines a node type. – Berdir May 16 '11 at 13:45
  • Oh my how embarrassing. I of course use the hook hook_node_insert and hook_node_update in a module I have to create, and perform necessary operations when the module type in question is created and updated. Thank you for the help Berdir. – pusle May 16 '11 at 13:45
1

Just wanted to add a comment on how I solved it in case anyone else needs it. This function ads a taxonomy term equal to the node title into a vocabulary defined in the function when a new node of your chosen type is inserted:

function node_title_taxonomy_node_insert ($node) {
if ($node->type == '<your node type>'):
    // Get vocabulary ID for vocabulary name 
    $vocabulary_name = "<Vocabulary name>";

    // Prepare taxonomy term
    $taxonomy_term_name = $node->title;

    // Add vocabulary and term
    _node_title_taxonomy_add_vocabulary_term($vocabulary_name,$taxonomy_term_name);
endif;
}

Thanks for the help Berdir :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.