0

I am using taxonomy module in my site and have used the "Add vocabulary" form by hook_form_alter().

I want to change the error message for machine name (Machine name in use) to some other message. This message and validation is thrown from form.inc function. How can I change this message in my hook_form_alter() function?

Some code

function resources_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {  
    if(strstr(current_path(), 'admin/structure/resources'))
    {       
        $form['#validate'][] = '_error_preservation_helper';

    }
}


function _error_preservation_helper($form, &$form_state) {
debug(form_get_errors());   
    $errors = form_get_errors(); // works shows error array 
    if(is_array($errors) && array_key_exists('machine_name', $errors))
    {
        // now change the error message
        form_set_error('machine_name', 'custom message', TRUE); // not works
    }
    debug(form_get_errors());   // same results 

}
3
  • U can set a from preprocess. Then in that preprocess function use form_get_error function to get all error elements and check if that machine_name element is in that. Then use drupal_get_messages function will give u all messages and it wont display messages. Then change the message content and display it again
    – Umar
    Commented Mar 27, 2012 at 12:03
  • This question really contains three questions that are not related to each other. I suggest you remove the last two questions, and create a new question for those.
    – avpaderno
    Commented Mar 27, 2012 at 12:04
  • Note: To add/change validation messages for form/fields, use Field validation module to create field based rule to display the error message which you want.
    – kenorb
    Commented Oct 30, 2015 at 15:28

3 Answers 3

1

It's not hook_form_alter() that can change the error message, since there isn't any error message when that hook is invoked. The error message is output from the form validation handler, which calls form_error(), or form_set_error() to set the error for the field that doesn't pass the validation.

If you want to change the error message for the specific form, and not globally, you can:

  • Add a new form validation handler that is executed after the default one.
  • In the validation handler, look for the error string in $_SESSION['messages'], and replace it with the string you want to show.

$_SESSION['messages'] is the variable where drupal_set_message() store all the messages it receives. drupal_set_message() is the function called by form_set_error(), which is the function called by form_error(). The minimal code the validation handler should execute is similar to the following one.

if (isset($_SESSION['messages']['error'])) {
  $messages = array_flip($_SESSION['messages']['error']);
  $message = t('The machine-readable name is already in use. It must be unique.');
  if (isset($messages[$message])) {
    unset($messages[$message]);
    $messages = array_flip($messages);
    $messages[] = t('your message error');
    $_SESSION['messages']['error'] = $messages;
  }
}

There is still a place where the error message is stored, and it is the static variable returned from form_get_errors(). It doesn't seem there is any module that looks for a specific value in that array; if there is any module doing that, for compatibility reasons is better not to change the error message reported in that array.

If the message error is used in a single form, you want to change that error message in any form, or there isn't any reason the error message is dynamically altered, then the easier way to change the error message is to:

  • Use the String Overrides module as Clive suggest.
  • Add the following code to settings.php.

    $conf['locale_custom_strings_en'][''] = array(
       'The machine-readable name is already in use. It must be unique.' => 'Your error message.',
    );
    

In the latter case, the string can be altered for any language; the following code changes the string for Italian.

$conf['locale_custom_strings_it'][''] = array(
   'The machine-readable name is already in use. It must be unique.' => 'Your error message, in Italian.',
);

Between the two solutions, the first is preferable because it doesn't require you to alter the settings.php file; the second is preferable because it doesn't require any module, and it is preferable if the strings to alter are not too much, and you don't have many languages enabled in the site.

1
  • Awesome explanation Very comprehensive reply many thanks
    – Umar
    Commented Mar 28, 2012 at 4:42
2

It's notoriously difficult to target/replace specific messages in the Drupal system. It's not impossible but it's a bit 'messy'.

I'd strongly recommend instead that you install the String Overrides module and use that to replace the specific message instead. String Overrides:

Provides a quick and easy way to replace any text on the site.

It is available for any string in the system which is passed through the t() function, which all core error messages are.

0
0

On top of what Clive and kiamlaluno said above, you can override the string for any language other than english by going to the Interface Translation Overview (URL depends on your Drupal version).

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.