2

I want to show a custom message to user after he/she adds a new content of a specific type in Drupal 7. If I change the '@type %title has been created' to what I want the message will change for all content types.

How can I do it? Is there any module to define messages for different actions?

4

3 Answers 3

3

I just did this the other day, it's a bit of a hack, but I couldn't find another way. The basic premise is to get and clear all current messages, loop through and call drupal_set_message() on all of them over again, changing the one you want:

<?php
/**
 * Submit function for the message node form.  Check for new message notification and change it.
 */
function mymodule_message_node_form_submit($form, &$form_state) {
  $node = $form_state['node'];
  $t_args = array('@type' => node_type_get_name($node), '%title' => $node->title);
  $replace_message = t('@type %title has been created.', $t_args);

  // This call will get all messages and clear them from $_SESSION
  $messages = drupal_get_messages('status');
  foreach ($messages['status'] as $message) {

    if ($message == $replace_message) {
      drupal_set_message(t('Thank you your message has been sent successfully.  We will be in touch shortly.'));
    }
    else {
      drupal_set_message($message);
    }
  }
}
?>
1

I am using this to replace an existing drupal message to my custom message.

function _exclude_message($mymessage, $mytype="status"){
$flag = false if ($messageArr = drupal_set_message()) {  foreach($messageArr as $type=>$messages) {
  if ($mytype==$type) {
    foreach($messages as $key=>$message) {
      if ($mymessage==$message) {
    unset($_SESSION['messages'][$type][intval($key)]);
       $flag = true;
      }
   }
  }
  if (count($_SESSION['messages'][$type])==0) unset($_SESSION['messages'][$type]);}} return $flag;}

function my_theme_status_messages($display = NULL) { if (_exclude_message('Sorry, unrecognized username or password. <a href="/user/password">Have you forgotten your password?</a>',"error")) drupal_set_message("Sorry, unrecognized username or password.","error"); return theme_status_messages($display); }

under template.php

this code will replace existing "Sorry, unrecognized username or password. " with my custom message "Have you forgotten your password?".

Hope this will help you.

0

I had to do that too. My solution is for error messages in validation hook. But should be quite the same for a confirmation message. You just have to find the id of the message (in my example the name of the field).

First I used a function to prevent the message from beeing sent.

function form_unset_error($name) {
   $errors = &drupal_static('form_set_error', array()); 
   $removed_messages = array();
   if (isset($errors[$name])) {
     $removed_messages[] = $errors[$name];
     unset($errors[$name]);
   }
   $_SESSION['messages']['error'] = array_diff($_SESSION['messages']['error'], $removed_messages);
   if (empty($_SESSION['messages']['error'])) {
     unset ($_SESSION['messages']['error']);
   }
}

So in my validation hook I unset the message and set a custom error message instead :

form_unset_error('field_name][und][0][value');
form_set_error('field_name', t('Custom error message translated'), $limit_validation_errors = NULL);

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.