2

I have a content type, 'A'. Now I need a form with the user registration form fields plus the 'A' creation form fields. So when the user (annonymous) submits this form, a user and a node of type 'A' with the filled information would be created.

I have this for now:

function my_module_menu() {
  $items['create_node_and_user'] = array(
    'page callback' => 'user_and_node_creation_form',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
  );

  return $items;
}

function user_and_node_creation_form() {
  module_load_include('inc', 'node', 'node.pages');
  $output = array();
  $output['user'] = drupal_get_form('user_register_form');
  $output['node_type_a'] = node_add('node_type_a');

  return $output;
}

The form is displayed fine, but How can I add a submit handler to manage the node creation after register the user?

1
  • What I see is you still use the original user register form. What you did is just put the form into your own page...
    – Tim Yao
    Commented May 13, 2015 at 23:31

3 Answers 3

3

The Create & Register module does this for you if you don't mind using a contrib module. From the project page...

This module creates a "lazy registration" which means that the user creates the content first and after this logs in or registers to the site.

Process

  1. anonymous user creates a content
  2. the user will be redirected to the login page (if you set it on the settings form) after he finished the node creation
  3. the user registers or logs in
  4. the created node(s) will be assigned to the user after logging in
2

Using hook_form_alter() to modify the original Drupal register form (form id:'user_register_form'), so you can change the user register form's fields.

Then just add a submit handler for this form to do something extra, like create a node.

$form['actions']['submit'][]='my_own_submit';

In your own submit function:

function my_own_submit($form, &$form_state){
  //create a node
  $node = new stdClass();
  $node->title = "YOUR TITLE";
  $node->type = "YOUR_NODE_TYPE";
  node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
  $node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
  $node->status = 1; //(1 or 0): published or not
  $node->promote = 0; //(1 or 0): promoted to front page
  $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write
  $node = node_submit($node); // Prepare node for saving
  node_save($node);
}
3
  • I would like not to modify the original form. I would like to create a new path instead. Could you see my updated question please? Thanks for the help.
    – Alfonso_MA
    Commented May 13, 2015 at 21:21
  • Sorry. I have made a mistake. I meant to say I would like not to modify the original 'path'.
    – Alfonso_MA
    Commented May 14, 2015 at 20:44
  • So you want to keep the original register form and create another page with a different form. This can be done by adding some condition in your hook_form_alter. Say if it's your 'create node & user' path, then alter the form to your one. Also, in your code, you added two forms in one page, you can't submit them together unless you use JS to help.
    – Tim Yao
    Commented May 14, 2015 at 23:41
1

There is also Inline Registration which looks promising:

  • Adds user_register() form to node/add pages if the user is not logged in
  • Associates the new piece of content with the new user
  • Can log the user in after node creation depending on user registration settings
  • Compatible with modules: email_registration, logintoboggan, registration_toboggan and other

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.