I am using References module to make a node reference in my course content type that reference to school content type (a course belong to one school and a school have many courses).

Now what I want is if I'm going to delete a school, the delete operation will not be completed if the school referenced by any courses.

I've read hook_node_delete() and hook_delete(), written them in my custom module, tried to dpm($node) but no use!

function mymodule_delete($node) {
function mymodule_node_delete($node) {

The school node still be deleted!

According to hook_node_info() , I create:

function mymodule_node_info() {
  return array(
     'school' => array(
      'name' => t('School'),
      'base' => 'school',
      'description' => t('A short description'),

base: (required) The base name for implementations of node-type-specific hooks that respond to this node type. Base is usually the name of the module or 'node_content', but not always

And then in my mymodule.module:

function school_delete($node) {

The code still not working!

  • please post the code that is not working – Geoff Jun 18 '15 at 4:48
  • Hi @Geoff , I use two hook functions above to dpm the $node, but nothing printed, school node always be deleted! – user48552 Jun 18 '15 at 6:21
  • I used your code and it worked fine for me. I assume you are using as an Admin, if not make sure the permissions for the user role allows access to Devel information. What theme are you using when access the delete page? – Geoff Jun 18 '15 at 7:49

I believe you are invoking the hook incorrectly - according to the API page for hook_delete():

This is a node-type-specific hook, which is invoked only for the node type being affected

Further disccussed on the Node API Hooks Page:

Node-type-specific hooks: When defining a node type, hook_node_info() returns a 'base' component. Node-type-specific hooks are named base_hookname() instead of mymodule_hookname() (in a module called '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 'poll' node type as 'poll'. So when a poll node is created, hook_insert() is invoked on poll_insert() only. Hooks that are node-type-specific are noted below.

  • Hmm.. I assume the node type is created without any problems? – Geoff Jun 18 '15 at 7:27
  • And did you clear the cache? – Geoff Jun 18 '15 at 7:29
  • Oh yes, the node type is created without problems and cached is cleared! – user48552 Jun 18 '15 at 7:31

If you are familiar with Rules (if you are not check here) you can use this selection :

add an integer field to school content type (this field is going to count courses which are referenced to a school)

on node creation event , check if it is a course type , if it is before saving that read the school reference value , then go and increase the school course count .

on node deletion , check if it is a course , then before deletion read the field school and decrease its course count value ,

and finally we are going to use some code :

//Implement hook_delete
function ModuleName_delete($node) {
   if($node->type == "school") //if your type machine readable name is school
       if($node->course_count[LANGUAGE_NONE][0]['value'] < 0) //check field name ...

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.