I use JFactory::getApplication()->enqueueMessage('Message goes here', 'error')
to show users the request could not be processed, it works OK but Joomla orders the messages in the sequence they occur. Because my message happens before the Joomla save error is captured, the user sees this message:
you cannot do this operation //my message
Save failed with the following error: //Joomla message
I want to invert the order and have Joomla message as it is, followed by my message so that it makes sense:
Save failed with the following error: // Joomla message
you cannot do this operation // my message
Is that possible? (without language translation or overrides?)
After help from answers, I could do the inversion: 1st message is a placeholder to be searched using getMessageQueue(). Although you could delete messages in J.2.5 it is no longer possible with J.3+ (https://developer.joomla.org/joomlacode-archive/issue-33270.html). The solution is to reflect the class to unprotect the queue and replace it.
public static function reorderMessages()
{
//error messages
$err01 = JText::_('COM_COMPONENT_MESSAGE1');
//you can adapt and add other messages here
$app = JFactory::getApplication();
$new_messages = array();
$replacement_found = null;
//mirror protected $_messageQueue
$appReflection = new ReflectionClass(get_class($app));
$_messageQueue = $appReflection->getProperty('_messageQueue');
$_messageQueue->setAccessible(true);
//get messages
$messages = $app->getMessageQueue();
foreach($messages as $key=>$message)
{
if($messages[$key]['message'] == 'MESSAGE_TO_REPLACE' && $messages[$key]['type'] == 'error' )
{
$replacement_found = 1;
continue;
}
$new_messages[] = $message;
}
if($replacement_found)
{
//save all messages
$_messageQueue->setValue($app, $new_messages);
//add replacement message to the end of the queue
$app->enqueueMessage(JText::_($err01, 'error');
}
return true;
}
Be very careful where to call the function, if the message queue is empty Joomla will return an error and break your code. Make sure you have enqueued the 1st message before calling the function.
You can use getMessageQueue()
on the application object (i.e., $myApp = JFactory::getApplication()
) to get a copy of the message queue array. You can clear the message queue by passing true
to the getMessageQueue()` function call. It will still return a copy of the system message queue array.
You could then use regex's to find the keys in the array and reorder them. I would find the system error message in the translation file, and use the error message key from the translation .ini
file (instead of the actual text of the error message) for the regex search so it doesn't break if the error message changes. I'd also do it in a plugin and on a late lifecycle hook (maybe the onBeforeRender
event).
You can save the modified message queue array back to the JApplication
class instance using the application object's enqueueMessage()
method, which has this signature:
enqueueMessage(string $msg, string $type = 'message') : void