0

I want to build a custom edit form for a existing node. The edit form should not show all fields and should be populated with the existing field values. Furthermore I want to add a few custom submit buttons. I know I can pre populate the fields with #default_value, but there are quite a lot of fields I have to pre populate like that, so I wanted to pre populate them all at once.

I have made a form function with the following code:

$node = (array)node_load($nid);
$form_state = array();
$form_state['values'] = $node;
$form = node_form($form_state, $node);
drupal_prepare_form('node_form', $form, $form_state);

I have also defined a custom submit button:

   $form['submit_one'] = array(
       '#type' => 'submit',
       '#value' => t('Gereed'),
       '#weight' => 10,
       '#submit' => array('custom_form_submit_one'),  
   );

And a submit handler:

 function custom_form_submit_one($form_id, $form_values) {
   watchdog ("Hello","Hello");  
 }

When I press the submit button, the submit handler is not called.

When I disable the statement:

$node = (array)node_load($nid);

and I press the submit button, the submit handler is called. However, my form is not pre populated with the values.

Is it possible what I want to do? Can I use a custom form and pre populate the fields (without specifying #default_value for each field)?

1 Answer 1

1

Why must you have a separate form? I think a simpler approach would be to modify the existing form on a conditional basis via a form alter hook:

function hook_form_alter(&$form, &$form_state, $form_id) {
  // match our custom content type ('yourContentType_node_form') edit form, and ensure
  // we're editing a specific node
  if ($form_id == 'yourContentType_node_form' && arg(1) == '123') {
    // add new submit buttons
    $form['actions']['submit_one'] = array(
      '#type' => 'submit',
      '#value' => t('Gereed'),
      '#weight' => 10,
      '#submit' => array('custom_form_submit_one'),
    );
  }
}
2
  • I need to develop about ten custom forms, each with their own requirements, info fields and submit buttons. That's why I didn't want to use the form alter to change the edit form. Besides, the normal edit form should also still be available. That's why I thought developing a few custom forms would be the right thing to do.
    – Paul
    Mar 6, 2013 at 23:12
  • Using the node id restriction in my example, you can expand on the code and create all of your different forms, without affecting the 'normal' version of this form. Mar 7, 2013 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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