[Drupal] How to save custom additions to the user object when a new user account is created in Drupal

| | 1 min read

This article explains how to programmatically, save custom additions to the user object when a new user account is created in Drupal. We can understand this with an example. Say, we want to create a default album with every newly created user, then we can use the following to save the custom additions. Similarly any of your additions can be done after referring the following.

  
  function photoshare_user_insert($edit, $account, $category) {
    $roles = $account->roles;
    if (in_array('photographer', $roles) || in_array('administrator', $roles)) {
      $album_name = "Default";
      $node = new stdClass();
      $node->type = 'album';
      $node->title = $album_name;
      $node->field_ps_album_description['und'][0]['value'] = 'Default album';
      $node->uid = $account->uid;
      $node->language = 'und';
      $node->status = 1;
      node_save($node);
    }
  }
  

In the above function, a new album is created using object class with title, description. Then the account's uid is given as node's uid and after setting the status to 1 to save the node. This will save the custom additions.