Friday, 1 May 2015

How to set a custom message using form_alter in Drupal











At times we need to change the message that is displayed after a node is saved. Following code helps to customize using form_alter.
  1. function mymodule_form_alter(&$form, &$form_state, $form_id)
  2. {
  3. if($form['form_id']['#value'] =='formvalue')
  4. {
  5. if($form['buttons']['submit']['#submit']){
  6. $form['buttons']['submit']['#submit'][] = 'mymodule_submit_function';
  7. }
  8. }
  9. }
  10. function mymodule_submit_function($form, &$form_state){
  11. global $_SESSION;
  12. $insert = empty($form_state['values']['nid']);
  13. if($insert){
  14. //Write your custom message text here
  15. if($form['nid']['#post']['form_id'] == 'formvalue'){
  16. $keys = array_keys($_SESSION['messages']['status']);
  17. $last_key = end($keys);
  18. $message = 'My custom messge after node save';
  19. $_SESSION['messages']['status'][$last_key] = $message;
  20. }
  21. }
  22. }


For such more Blogs you can visit to http://findnerd.com/NerdDigest

No comments:

Post a Comment