3

I'm modyfing a node form to add a few extra fields. On submit, I need to program some extra logic to these values then save them to the node object.

function module_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'my_node_form') {
    $form['custom_form'] = array(
     '#type' => 'textfield',
     '#title' => t('Custom field'),
     '#default_value' => 'enter custom text here',
    );

    $form['#validate'][] = 'custom_form_process_values';  
  }
 }


 function custom_form_process_values(&$form, &$form_state, $form_id) {
    echo '<pre>' . print_r($form_id,1) . '</pre>';
    echo '<pre>' . print_r($form_state,1) . '</pre>';
    exit();
  }

When I go to node/22/edit and change "enter custom text here" to something else, when I submit the form I only ever see "enter custom text here" and not the new text I entered. It seems that it does not carry the text through to the validate function. Also tried it in a submit function but the same problem occurs.

Any ideas why this may be?

Using Drupal 7.

0

This pair of functions should do the trick:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (stripos($form_id, 'node_form') !== FALSE) {
    $form['custom_form'] = array(
     '#type' => 'textfield',
     '#title' => t('Custom field'),
     '#default_value' => 'enter custom text here',
    );
    $form['#validate'][] = 'mymodule_custom_form_process_values';
  }
 }

function mymodule_custom_form_process_values($form, &$form_state) {
  // Check to see if the default text has been altered by the user.
  if ($form_state['values']['custom_form'] != $form['custom_form']['#default_value']) {
    // Do something here...
  }
}

Tested on Drupal 7.53.

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.