Created
October 19, 2012 16:07
-
-
Save purdy/3919058 to your computer and use it in GitHub Desktop.
Drupal drupal_write_record() recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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