Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I would like to change the default error message from Drupal core.

The error message is "Please Select the Valid File Format".

My problem is in brief below.

  • I have created a CCK called 'Assignment'.
  • There I have given the users an option to upload files.
  • In such case if the user uploads invalid file format drupal will throw the error message as mentioned above.
  • I need to change the error message in to "The file upload field cannot upload the speicified filed".

How can I achieve this?

(Should not use string overrides module)

Thanks in advance..

share|improve this question

4 Answers 4

up vote 3 down vote accepted

If that string is run through the t() function (which is probably is) and you don't want to use String Overrides, then you can update your settings file.

For Drupal 6, putting this in your "settings.php" file should work.

$conf["locale_custom_strings_en"] = array(
  "Please Select the Valid File Format" => "The file upload field cannot upload the speicified filed",
);

In Drupal 7, you would change it to

$conf["locale_custom_strings_en"][""] = array(
  "Please Select the Valid File Format" => "The file upload field cannot upload the speicified filed",
);

However, I am not seeing the string "Please Select the Valid File Format" in any of my D6 installs, and that is not the message I get when try to upload to a CCK filefield with the wrong file type or the MIME type doesn't match the file extension.

share|improve this answer
  1. Get the messages using drupal_get_messages(). Make sure to leave the $clear_queue parameter at the default true.
  2. Search the resulting array for the relevant messages and replace them. The resulting array is indexed by message type and the values are arrays of messages for that type (even if you only retrieve messages of a particular type).
  3. Inject the messages back into the message queue using drupal_set_message().

Drupal retrieves the message queue in function theme_status_messages() which is called from template_preprocess_page(), so one place to achieve this would be an override of that theme function. Depending on the exact circumstances of the message, you might find some hook that is closer to the point when the message is generated.

Because the messages are rendered in template_preprocess_page(), an implementation of hook_exit() is not an option, because this hook is invoked when the page is already rendered.

BTW: I often wished for a hook_message_alter().

share|improve this answer

I placed the following code in template_preprocess_page() and it worked perfectly. I'm not totally convinced this is the best way to do it but it was faster than building a module to generate the custom errors.

To get this done I looked at all the error messages generated by drupal_get_messages() and cycled through them. If they matched the string I was looking for I would then use drupal_set_message() to generate the error with new text. The final step was to pass the remaining errors through drupal_set_message() otherwise they wouldn't be called.

Again I'm not certain this is the best way of pulling it off, if anyone has any suggestions on how to do this in the theme layer I'd love to hear them.

$getmsg = drupal_get_messages('error');
    if ($getmsg){
      foreach($getmsg['error'] as $err) {

          switch($err) {
          case 'SUBSCRIBE TO THE NEWSLETTER field is required.':
            drupal_set_message('Sorry, you must sign up to the newsletter to enter', 'error');
          break;
          case 'I AGREE WITH THE CONTEST RULES &amp; REGULATIONS</a> field is required.':
            drupal_set_message('Sorry, you must agree to the rules and regulations', 'error');
          break;
          default:
            drupal_set_message(t($err), 'error', FALSE);
          break;
        }

      }
    }
share|improve this answer

The easiest way is to use the String Overrides module.

Install the module and you can replace the string with your own string. The string overrides module also allows you to replace any string wrapped in the t() function.

share|improve this answer
    
The OP specifically stated, not using the String Overrides module –  Laxman13 Sep 29 '11 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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