2

At the time when a new user is created, I want programmatically to create a new node (of some defined type) and make him the owner of that node.

How can I do this in Drupal 7 ?

3 Answers 3

5

It is correct that you need to implement hook_user_insert(). The code that needs to be implemented is different from the code you would implement in Drupal 6 because the node body is a field, in Drupal 7 (see Field API).

The implementation of hook_user_insert() should be similar to the following.

function mymodule_user_insert(&$edit, $account, $category) {
  $body_text = 'This is the text contained in the node body.'; // [1]
  $node = new stdClass();
  $node->type = 'article';  // [1]
  node_object_prepare($node);

  $node->language = LANGUAGE_NONE;

  $node->body[$node->language][0]['value']   = $body_text;
  $node->body[$node->language][0]['summary'] = text_summary($body_text);
  $node->body[$node->language][0]['format']  = 'filtered_html';

  $node->title    = 'Node Created Programmatically on ' . date('c'); // [1]
  $node->uid      = $account->uid;

  $path = 'content/programmatically_created_node_' . date('YmdHis'); // [1]
  $node->path = array('alias' => $path);

  node_save($node);
}

The lines marked with [1] are the parts that you need to personalize.

0
2

This is done pretty much as Drupal 6. Create an object with the required values, and use node_save. This should all happen in hook_user op insert:

module_user_insert(&$edit, $account, $category) {
  $node = new stdClass();
  $node->title = 'title'
  $node->uid = $account->uid;
  node_save($node);
}
0
1

You will need to create a new node inside an implementation of hook_user_insert.

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.