I'm trying to use a combination of hook_form_alter() and hook_nodeapi() to add a custom field to an existing Drupal form, save the value and load that value for use. Am I on the right path with these functions?

Here's what I'm trying:

<?php
function my_module_form_alter(&$form, $form_state, $form_id) {
    
  switch ($form_id) {
    case '/*the form ID*/':
      
      $form['my_field'] = array(
        '#title' => t('My Title'),
        '#description' => 'My description.',
        '#type' => 'checkboxes',
        '#options' => _my_module_options(), // returns array of options
        '#default_value' => _my_module_default($form_id), // loads default value
        '#weight' => 0,
      );
      break;
  }
}
?>

...along with:

<?php
function my_module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
      // insert routine
      break;
    case 'update':
      // update routine
      break;
    case 'delete':
      // delete routine
      break;
    case 'view':
      // view routine
      break;
  }
}
?>

My custom field is showing up in the form I want to alter but the data is not being saved. How do I go about saving the data from my custom field? Do I add another $form[#submit]? I've seen the following used inside of the hook_form_alter() function:

<?php
$form[#submit][] = array('my_custom_submit_function_submit');
?>

But I can't seem to get it to work. Nothing's being written to my custom table. Or should I be putting the submit function in the hook_nodeapi()'s 'update' or 'insert' cases?

Comments

ashsc’s picture

So the correct way (at least a way that seems to be working) is to add the following code inside the my_module_form_alter() function:

<?php
$form['#submit'][] = array('my_custom_submit_function_submit');
?>

Where 'my_custom_submit_function_submit' is the function I write to handle inserts into my database table.

This pushes my function into the array of functions being executed when the form is submitted. I'm unclear if my function needs to end in '_submit' or if that's just a best practice suggestion.

I still, however, do not understand what the hook_nodeapi() function does or when it's called. The documentation states:

It is common to find hook_nodeapi() used in conjunction with hook_form_alter(). Modules use hook_form_alter() to place additional form elements onto the node edit form, and hook_nodeapi() is used to read and write those values to and from the database.

So why am I using a $form['#submit'][] function assignment when the documentation states that hook_nodeapi() handles that part? When I write my database routine in hook_nodeapi() (case: 'insert') it doesn't seem to be called at all. Should I keep using $form['#submit'][] or is there a better way? What is the purpose of hook_nodeapi()?

ferrum’s picture

I had the same problem and searched in books and official documentation but found only many questions regarding this issue and some hints how it could be solved. My solution now is

<?php
$form[#submit][] = 'my_custom_submit_function_submit';
?>


and it works fine for me not using an array as in the other examples in this thread. What's the difference for Drupal?

I also wonder whether it is not mentioned in the hook_form_alter() documentation how to validate and save the changed or added elements?

teamred’s picture

for me what worked (in drupal 5) was

$form['#submit']['my_custom_submit_function_submit'] = array();