Skip to main content
added 370 characters in body
Source Link
Andre Baumeier
  • 2.7k
  • 1
  • 14
  • 37
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';
   }
}
// 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);
}
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['#submit'][] = 'mymodule_user_register_create_node';
   }
}
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);
}
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';
   }
}
// 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);
}
added 370 characters in body
Source Link
Andre Baumeier
  • 2.7k
  • 1
  • 14
  • 37

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);
    }
  }

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);
    }
  }
added 370 characters in body
Source Link
Andre Baumeier
  • 2.7k
  • 1
  • 14
  • 37

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['#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.

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.

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['#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.

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.

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['#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.

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.

Source Link
Andre Baumeier
  • 2.7k
  • 1
  • 14
  • 37
Loading