Moving node form fields in a fieldset (without field_group module)

Public

- Replace MYMODULE by your module name
- Replace node_type by your node type name
- Replace group_name by your fieldset machine name
- Replace Group name by your fieldset human readable name
- Replace field_fieldname by your field machine name

Get raw version
php
  1. /**
  2. * Implements form_alter().
  3. */
  4. function MYMODULE_form_alter(&$form, &$form_state) {
  5. if( $form['#form_id'] == 'node_type_node_form' ){
  6. // Implement afterbuild
  7. $form['#after_build'][] = '_MYMODULE_form_organize';
  8. // Implement validate to avoid errors & notices
  9. $form['#validate'][] = '_MYMODULE_form_validate';
  10. }
  11. }
  12. /**
  13. * Afterbuild function
  14. */
  15. function _MYMODULE_form_organize($form, $form_state){
  16. // Create fieldset
  17. $form['group_name'] = array(
  18. '#type' => 'fieldset',
  19. '#title' => t('Group name'),
  20. '#weight' => '10'
  21. );
  22. // Move field in fieldset
  23. $form['group_name']['field_fieldname'] = $form['field_fieldname'];
  24. // Unset default field instance
  25. unset($form['field_fieldname']);
  26.  
  27. return $form;
  28. }
  29.  
  30. /**
  31.  * Validate function
  32.  */
  33. function _MYMODULE_form_validate(&$form, &$form_state) {
  34. $form['field_fieldname'] = $form['group_name']['field_fieldname'];
  35. }

Comments

Zolotyx's picture

After form_set_error in form validate, fields return to previous places