How to create a new custom content type record when a user registers My client's Drupal 7 website has a custom content type which is currently being used as a user profile. Now we need to incorporate this custom content type with the Drupal registration process. That is to create a user profile(a new record in custom content type) when a user registers. The User profile content type has many fields which asks for personal details and there are many views that are based on this content type. Therefore, I need to have this content type as it is but to incorporate this somehow with the Drupal registration process.

Is this doable?
Since you are deeply in your project I see two possibilities right now:


Site builder


Add all the required fields to your user profile, and use something like complete_profile to force completion. Maybe profile2 might help as well. Once you gathered a complete profile use rules to create a node of the content type you strive to create. Upon creation, you might want to drop the profile field contents etc., I think profil2 might be the better solution, so you don't have the to use drupals core profile, and can drop that filled profile once you don't need it, or even disallow changes after creation / hide fields of it etc.


Developer


try to get the node fields of the node add form for your specific content type and merge them with the registration form.

function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if ($form_id == 'user_register') {
     dpm($form);
     module_load_include('inc', 'node', 'node.pages');
     $node_form = node_add('nodetype');
     // $output = drupal_render($form);

     // this copying probably has to be done in a better way, e.g. by only the fields you need.
     $form['node'] = $node_form;

     // register a submit handler, so you can create the node/ do validation whatsoever
     $form['#validate'][] = 'mymodule_user_register_validate';
     $form['#submit'][] = 'mymodule_user_register_create_node';
   }
}


If your node form has a file upload widget, you should add the following lines to the menu array:

'file path' =&gt; drupal_get_path('module', 'node'),
'file' =&gt; 'node.pages.inc',


submit handler:

There are probably better ways to do this, e.g. reuse the existing node submit handler, instead of mimicking it's behavior.

// validation option 1: do it manually
function mymodule_user_register_validate($form, &amp;$form_state) {
  // Extract the submitted data
  $name = $form_state['values']['node_title'];
  // Check it according to your own logic
  $is_valid_name = your_check_for_valid_name();
  // File error, when not valid
  if (!$is_valid) {
    form_set_error('name', t('Name already taken, please choose a different one'));
  }
}

// validation option2: forward it to node_submit
// this way you will have to mock a node object before though.
// node_validate($node, $form, &amp;$form_state);

function mymodule_user_register_create_node($form, &amp;$form_state) {
  $node = new stdClass();
  $node-&gt;type = 'nodetype';
  $node-&gt;title = $form_state['values']['node']['title'];
  $node-&gt;language = LANGUAGE_NONE;
  $node-&gt;uid = 0; // you probably have to somehow find out the created uid from the registration and link this here
  node_object_prepare($node);
  $node-&gt;field_something[LANG_NONE][0]['value'] = $form_state['values']['node']['sometextfield'];
  node_save($node);
}


This code is far from being ready to use (I just drafted it straight from my head, you will have to dpm() some values and see how it fits together, but the logic/ workflow is there.

extra:
look at how entity_form_submit_build_entity(); is used to see how core get's the form, form state values and creates an entity with the given input, instead of doing this by hand. Be aware that at that stage you probably have to make sure that everything required is available, which might be harder than doing that stuff in custom code in the first place.

extra2:
to link this created node with the user created, you might want to put the created node id in global space and retrieve it in a hook that is called after user save.

  // mymodule_user_register_create_node() ...
  node_save($node);
  $GLOBALS['user_register_linked_nid'] = $node-&gt;nid;
  // ...

  function mymodule_user_insert(&amp;$edit, $account, $category) {
    //maybe the submitted values are in $edit as well, than we dont have to do the node creation stuff in the user submit handler and we could do it here.
    // else you can still fetch the global variable
    if (isset($GLOBALS['user_register_linked_nid'])) {
    $account['field_node_reference'][LANG_NONE][0]['target_id'] = GLOBALS['user_register_linked_nid'];
    field_attach_insert('user', $account);
    }
  }