2

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?

5
  • sounds a lot like drupal.org/project/profile2 should suit your needs, only problem might be that you have your stuff already setup using the other approach Commented Mar 30, 2015 at 0:23
  • Yes, It is now too late to use that module. What I need to happen now is to register new users as it does normally and at the same time create a new record in the custom content type as well. Registration form needs to be customized to add fields which are needed for the custom content type. Commented Mar 30, 2015 at 0:34
  • not sure what you are, and what knowledge you bring with you, I could think of two solutions, see in my answer Commented Mar 30, 2015 at 1:06
  • Thanks for your time Andre. I'm comfortable with PHP and have overridden some files inside the custom theme of this site and also have created a module(just for this site) to achieve few things. It would be great if you could give me some tips on this one. Commented Mar 30, 2015 at 1:22
  • did some explanation below, this probably will take a while for you to try (in case your skills are good enough to try the developer way), let me know how it goes. Commented Mar 30, 2015 at 1:52

2 Answers 2

1

Since you are deeply in your project I see two possibilities right now:

  1. 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.

  1. 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(&$form, &$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' => drupal_get_path('module', 'node'),
'file' => '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, &$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, &$form_state);

function mymodule_user_register_create_node($form, &$form_state) {
  $node = new stdClass();
  $node->type = 'nodetype';
  $node->title = $form_state['values']['node']['title'];
  $node->language = LANGUAGE_NONE;
  $node->uid = 0; // you probably have to somehow find out the created uid from the registration and link this here
  node_object_prepare($node);
  $node->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->nid;
  // ...

  function mymodule_user_insert(&$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);
    }
  }
1
  • Thank you so much for your detailed explanation. I really appreciate your help. I will work on this soon as I get a chance and get back to you. Commented Mar 30, 2015 at 2:00
0

Rules module can be used here. 1. ON "After saving a new user account" - Perform actions like create entity of type node. set field values etc. Using rules interface. You don't have to write code for that. 3. ON "After updating an existing user account" - update the user profile fields. 4. ON "After a user account has been deleted" - delete the node entity. check this https://www.drupal.org/project/rules

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.