2

I have a node form in Drupal 7, in order to simplify it for the user I want to break it up into sections using the vertical tabs feature.

Using hook_form_FORMID_alter() I can move the fields with difficulty.tha following is my code.:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'test_node_form') {     
    $form['cpmonth'] = array(
      '#type' => 'fieldset',
      '#title' => t('test'),
      '#weight' => 5,
      '#collapsible' => TRUE,
    );
$form['cpmonth']['field-14sq']=array();
$form['cpmonth']['field-510sq']=array();


  }

}

"field-14sq" and "field-510sq" are my input text fields. Do you have any idea?

  • 1
    The question is too broad. Changing the form fields of an existing form is not different from creating your own form; the only difference is that the form already exists. – kiamlaluno May 24 '11 at 15:34
5

If I understand correctly you want to move existing form elements into the fieldset you created. Try this, I'm assuming the fields you want to move are $form['field-14sq'] and $form['field-510sq']

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'test_node_form') {     
    $form['cpmonth'] = array(
      '#type' => 'fieldset',
      '#title' => t('test'),
      '#weight' => 5,
      '#collapsible' => TRUE,
    );
    $form['cpmonth']['field-14sq'] = $form['field-14sq'];
    unset($form['field-14sq']);
    $form['cpmonth']['field-510sq'] = $form['field-510sq'];
    unset($form['field-510sq']);
  }
}

If you have custom submit and validation functions this shouldn't affect them, since fields in fieldsets still come back in the root level of $form_state['values'] .

2

what about using the Field Group module Start with creating the node that has the field groups attached to it. Then the each Field Appears like a very cool edit in place feature. It's a cool way of looking at that problem.

1

i would print each input individually with something like :

print drupal_render ($form['cpmonth']['field-14sq'])

This way you have lot of flexibility and then can organize your form the way you want.

Although make sure to also print the form ID and tokens to be able to validate the form using that techinc.

print drupal_render($form['form_id']);
print drupal_render($form['form_build_id']); 
print drupal_render($user_edit['form_token']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.