Problem
I'm attaching the user register form to the add node form using hook_form_alter (for a custom content type). (Forcing an anonymous user to register when they create a node.)
My solution to this problem, has been to put a large chunk of the user register code into hook_form_alter
(because I'm a "great" developer).
While this works in a custom form, in hook_form_alter
the following function
field_attach_form('user', $form['#user'], $form, $form_state, $langcode);
Causes the register fields to always appear in the middle of the form. (#weight does nothing.)
Reading through the documentation, it appears this function always puts the fields at the top of $form_state['values']
and you can change it by setting the #parents
property on the incoming $form
parameter.
2 Questions:
Why does the order in
$form_state
effect the display of my form? I thought the#weight
property in the form elements determined this.I don't really understand how to set the parents property. Do I need to create a parent which encompasses all of the standard node add form, and then set this new form relative to that?
More code in case it's wanted.
mymodule_form_alter (&$form, $form_state, $form_id) {
...
if (user_is_anonymous($user)) {
$admin = user_access('administer users');
$form['administer_users'] = array(
'#type' => 'value',
'#value' => $admin,
);
$form['#user'] = drupal_anonymous_user();
$form['#user_category'] = 'register';
$form['#attached']['library'][] = array('system', 'jquery.cookie');
$form['#attributes']['class'][] = 'user-info-from-cookie';
$account = $form['#user'];
$register = ($form['#user']->uid > 0 ? FALSE : TRUE);
$form['#validate'][] = 'user_account_form_validate';
$form['account'] = array(
'#type' => 'container',
'#weight' => 61,
);
.... //form elements for user reg like password, username email etc. go in here...
$langcode = entity_language('user', $form['#user']);
field_attach_form('user', $form['#user'], $form, $form_state, $langcode);
foreach (field_info_instances('user', 'user') as $field_name => $instance) {
if (empty($instance['settings']['user_register_form'])) {
$form[$field_name]['#access'] = FALSE;
}
}
}