I would like to have my module to create new nodes (page) when being installed and removing these pages on uninstall.

I tried the following with no success in my mymodule.install file:


function mymodule_install() {

  $node = new stdClass(); // We create a new node object
  $node->type = "page"; // Or any other content type you want
  $node->title = "Your title goes jere";
  $node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
  $node->uid = 1; // Or any id you wish
  $node->path = array('alias' => 'your_node_path'); // Setting a node path
  node_object_prepare($node); // Set some default values.

  // Let's add standard body field
  $node->body[$node->language][0]['value'] = 'This is a body text';
  $node->body[$node->language][0]['summary'] = 'Here goes a summary';
  $node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field

  $node = node_submit($node); // Prepare node for a submit
  node_save($node); // After this call we'll get a nid  


This code is cut n' paste from this page.

When I disable/enable the module, the page is not created (at least it does not appear in Content and in the node table of the database).

How should I do it please?

1 Answer 1


The problem is that install/uninstall and enable/disable are two separate things in Drupal.

When you enable or disable a module, hook_enable() and hook_disable() are invoked respectively.

When you install or uninstall a module, hook_install() and hook_uninstall() are invoked respectively.

If you want to keep your code in hook_install() / hook_uninstall() you'll need to disable the module, and then uninstall it at http://mysite.com/admin/modules/uninstall. Once you re-install it (which will also enable it), your code will be run.

It might be worth having a think if this code would be better served in hook_enable() / hook_disable(), but it will depend on your particular situation.

  • 1
    Sorry for my misunderstanding of the API Clive. And thank you for your help, it works beautifully. I will put in hook_disable() some lines to delete the content created in the _enable() function now. Do you have any recommendation or good practice to keep track (in hook_enable()) of the node ID to be deleted (in hook_disable())?
    – Pascal
    Commented Jun 11, 2012 at 7:41
  • 1
    Note: in Drupal 8 these methods have been consolidated. There is no hook_enable or hook_disable anymore, just hook_install and hook_uninstall. See details here: drupal.org/node/2193013
    – bryanbraun
    Commented Jun 20, 2017 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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