tags:

views:

210

answers:

2

I am trying to append a string to the body field of a CCK node after it has been submitted or edited. However, I'm having trouble working with the body field in the form alter. My initial attempt was to modify the body field in the submit handler by using the .operator to append a string to the body field.

//Calling this submit function to add string to body.
function appendToBody_submit_function($form, &$form_state) {
$form_state['values']['body'] = array('0' => array('value' => $form['#body'])) . $stringToAppend;
}

However, I can't get this to work, and I'm not sure it's the right way. I am new to Drupal, Can someone point me in the right direction? Should I be using node_api for this?

A: 

I recommend installing the Devel module so you can easily print out the contents of $form_state by placing dpm($form_state); in your method body. I usually start with that to make sure the values are where/what I expect.

// Replace "hook" in the function name with the name of your module.
function hook_submit($form, &$form_state) {
  // dpm($form_state); // Debug code to view the contents of $form_state.
  $body = $form_state['values']['body'] . ' new string to append';
  // Place code to save this data to your database here.
}
jwhat
@jwhat - By "Place code to save this data to your database here." - do you mean an insert/update? I have always just manipulated the $form_state variables. If it's an update, I see how that can work, but I don't know how that would work if it was a new node?
newGuy
There should be no need to save the data explicitly if the manipulation happens before the standard submit handler gets called, see my separate answer for that.
Henrik Opel
Yes, you shouldn't need to save the data explicitly in this case. You may just need to change your assignment to (as Henrik stated): $form_state['values']['body'] = $form_state['values']['body'] . $stringToAppend;
jwhat
A: 

I assume that you add your custom submit callback to the forms #submit array via hook_form_alter().

If you add it before any other entry in that array (as opposed to just appending it), your callback should be called before the standard submit function. That way, all you need to do is adjust the $form_state['values']['body'] content 'in place', and it will be picked up (and subsequently saved) on further processing by the standard submit callback implicitly:

/**
 * Implementation of hook_form_alter()
 */
function yourModule_form_alter(&$form, $form_state, $form_id) {
  // Is this a node edit form?
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    // Yes, add custom submit handler *before* already existing ones
    array_unshift($form['#submit'], 'appendToBody_submit_function');
  }
}

// Custom submit function to add string to body.
function appendToBody_submit_function($form, &$form_state) {
  $form_state['values']['body'] = $form_state['values']['body'] . $stringToAppend;
}
Henrik Opel