array( 'name' => t('2-step node'), 'module' => 'twostepnode', 'description' => t('A node that uses a multi-step node form for testing.'), ), ); } /** * Implementation of hook_form(). */ function twostepnode_form(&$node, $form_state) { $form['title'] = array( '#title' => t('Title'), '#type' => 'textfield', '#required' => TRUE, '#default_value' => isset($form_state['storage']['title']) ? $form_state['storage']['title'] : $node->title, ); $form['body_field'] = node_body_field($node, t('Required body'), 1); return $form; } /** * Implementation of hook_form_alter(). * * When first adding the node, put the 'Title' field on its own page in a * multistep node form. We need to do this in hook_form_alter() itself * instead of a form_id-specific form alter so that we're likely to come after * everyone else's alterings, e.g. taxonomy, menu, etc. */ function twostepnode_form_alter(&$form, $form_state, $form_id) { if ($form_id == 'twostepnode_node_form') { $node = $form['#node']; if (empty($form_state['storage']['title']) && empty($node->title)) { _twostepnode_step1($form, array('title')); } else { // For the actual submit button, add a handler to clear out // $form_state['storage']['title'] so that we actually submit the form. $form['buttons']['submit']['#submit'][] = 'twostepnode_node_form_final_submit'; } } } /** * Helper function to convert the node form into step 1 of a multi-step form. * * Marks all form elements to #access FALSE except those required on step 1, * converts the usual "preview" button into a "next" button and hides the * submit button. */ function _twostepnode_step1(&$form) { // Hide all the elements we don't want on step 1. foreach (element_children($form) as $child) { if ($child != 'buttons' && (empty($form[$child]['#type']) || ($form[$child]['#type'] != 'hidden' && $form[$child]['#type'] != 'value' && $form[$child]['#type'] != 'token' && ($child != 'title')))) { $form[$child]['#access'] = FALSE; } } // Change preview to next and hide submit. $form['buttons']['preview'] = array( '#type' => 'submit', '#value' => t('Next'), '#weight' => 50, '#submit' => array('twostepnode_node_form_next_handler'), ); $form['buttons']['submit']['#access'] = FALSE; } /** * Submit handler for the 'Next' button on step 1. * * Saves the title value into $form_state['storage']. */ function twostepnode_node_form_next_handler($form, &$form_state) { if (isset($form_state['values']['title'])) { $form_state['storage']['title'] = $form_state['values']['title']; } } /** * Submit handler for the "Save" button on step 2. * * Clears out $form_state['storage']['title'] so the form will submit and will * not rebuild itself. */ function twostepnode_node_form_final_submit(&$form, &$form_state) { unset($form_state['storage']['title']); }