0

I am rendering node form on my custom page with such a code:

<?php
 $form_state['build_info']['args'] = array($node);
 form_load_include($form_state, 'inc', 'node', 'node.pages');
 $form = drupal_build_form($type . '_node_form', $form_state);
 return drupal_render($form);
 ?>

I need to add some custom form elements to the form as follows:

 <?php
  $form['mycode'] = array(
 '#type' => 'textfield',
 '#title' => t('My code'),
 );
 ?>

The custom form elements are displayed correctly in the form. The problem is, when I submit the form, I cannot see custom values in $form_state or even $_REQUEST array. I am checking $form_state values in the implementation of hook_node_submit

What is the correct way to add custom form elements to node form (except creation of fields) ? (I need custom form elements because I need to use different field widgets for various user roles. I also need to add checkbox like "I have read Terms and Conditions" into node form.)

thanks Tomas

  • 2
    You need to use hook_form_alter() to manipulate forms – Clive Apr 12 '14 at 9:19
  • I have added last line to my page callback in question. ( return drupal_render($form); ) As I wrote to RajeevK, it seems like node forms have some restrictions to add custom form elements. Or there is no such restriction and I should search for bug in my code? – tomas.teicher Apr 12 '14 at 12:17
  • No there are no restrictions like that on node forms, you can add any elements you like. Their values obviously won't be saved in the database or anything unless you write the code to do that, but they'll definitely show up on the form. – Clive Apr 12 '14 at 12:20
2

You need to use hook_form_alter for this requirement. You can create a submit function for the same & declare it as submit function for previously created alter function. You can access $form_state into that.

In same way you can have your own validate function etc.

  • thanks for the answer, I have tried to add custom form elements in hook_form_alter, but these elements are not showing in the form. It is strange, I do not know if there is some restriction to node forms? I have tested that program run through my hook, but my changes are not applied in the node form. – tomas.teicher Apr 12 '14 at 12:13
  • There must be some issue with code :) – RajeevK Apr 12 '14 at 13:00

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.