Wednesday, November 10, 2010

Hook nodeapi presave in Drupal 6

There are 2 positions of the node before we change the property, ie, when the insert node (node doesn't exist before) and at the moment update. We use the hook nodeapi with $op = 'presave', either insert or update, both will pass hook nodeapi $op = 'presave'.

We just take advantage of CCK 'story' for example. We'll be programmatically change node properties such as:

Title: 'Test nodeapi presave'.
Language: Indonesian (formerly install Local module, and then at Administer > Site Configuration > Language, add the Indonesian.
Author: yogarta (previously add the user with username: yogarta, for example this uid is 3 (change with your own).

If you already have a home-made module (please make a simple module, at least consist of .info and .module files). Open the .module files, for example, we use the module with the name 'test'. Add the following script:

function test_nodeapi($node, $op) {
  if ($node->type == 'story') {
    switch ($op) {
      case 'presave':
      $node->title = 'Test nodeapi presave';
      $node->language = 'id';
      $node->uid = 3;
      $node->name = 'yogarta';
      break;
    }
  }
}

Well now you try to create / edit a content type of 'story' and then check whether there are any changes from content page.