Skip to content

Instantly share code, notes, and snippets.

@purdy
Created October 19, 2012 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purdy/3919058 to your computer and use it in GitHub Desktop.
Save purdy/3919058 to your computer and use it in GitHub Desktop.
Drupal drupal_write_record() recipe
function MYFORM_submit( $form, &$form_state ) {
$msg = '';
$msg_type = 'status';
$redirect = 'admin/MYMODULE/INDEX_PATH';
$action = 'save your changes';
$expected = SAVED_UPDATED;
$update_msg = 'Your changes to the %category category have been saved.';
if ( is_numeric( $form_state['values']['id'] ) ) {
$TABLE_DATA['id'] = $form_state['values']['id'];
$result = drupal_write_record( 'MYTABLE', $TABLE_DATA, 'id' );
}
else {
$result = drupal_write_record( 'MYTABLE', $TABLE_DATA );
$expected = SAVED_NEW;
$action = 'create the new category';
$update_msg = 'The %category category has been created.';
}
if ( $result === $expected ) {
$msg = t( $update_msg, array( '%category' => $TABLE_DATA['category'] ) );
}
else {
$msg_type = 'error';
$msg = t( "There was a problem when trying to $action. You can " .
"either try again or let the IT dept. know there's a problem." );
$redirect = '';
}
drupal_set_message( $msg, $msg_type );
$form_state['redirect'] = $redirect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment