Difference between revisions of "Display error messages and notices"
From Joomla! Documentation
(Additions for J! 3.x, remove refs to PHP 4) |
(Several markup changes. URL corrections.) |
||
(16 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | + | <noinclude><languages /></noinclude> | |
− | Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below. | + | <translate><!--T:1--> |
− | < | + | Errors, warnings, alerts and notices can be displayed from any component, module, plugin or template using the methods outlined below.</translate> |
+ | |||
+ | <syntaxhighlight lang="php"> | ||
// Get a handle to the Joomla! application object | // Get a handle to the Joomla! application object | ||
$application = JFactory::getApplication(); | $application = JFactory::getApplication(); | ||
// Add a message to the message queue | // Add a message to the message queue | ||
− | $application->enqueueMessage(JText::_(' | + | $application->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error'); |
/** Alternatively you may use chaining */ | /** Alternatively you may use chaining */ | ||
− | JFactory::getApplication()->enqueueMessage(JText::_(' | + | JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error'); |
− | </ | + | </syntaxhighlight> |
− | The second argument to the [ | + | <translate><!--T:2--> |
− | < | + | The second argument to the [https://api.joomla.org/cms-4/classes/Joomla-CMS-Application-CMSWebApplicationInterface.html#method_enqueueMessage enqueueMessage] function is the type of the message. The default is ''message'', but ''error'' results in a different style for the message. The message will be displayed in place of a special ''jdoc:include'' statement in your template. Place the following in your template at the location where you want messages to appear.</translate> |
+ | |||
+ | <syntaxhighlight lang="php"> | ||
<jdoc:include type="message" /> | <jdoc:include type="message" /> | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | <translate> | ||
+ | == Message == <!--T:3--> | ||
+ | </translate> | ||
− | + | <div style="background-color: #dff0d8; color: #3c763d; border-top: 3px solid #d6e9c6; border-bottom: 3px solid #d6e9c6; padding-left: 1em; font-weight: bold;"> | |
− | <div style="background-color: # | ||
Message | Message | ||
</div> | </div> | ||
− | < | + | <syntaxhighlight lang="php"> |
JFactory::getApplication()->enqueueMessage('Message'); | JFactory::getApplication()->enqueueMessage('Message'); | ||
− | </ | + | </syntaxhighlight> |
− | == Notice == | + | <translate> |
+ | == Notice == <!--T:4--> | ||
+ | </translate> | ||
− | <div style="background-color: # | + | <div style="background-color: #c3d2e5; color: #0055bb; border-top: 3px solid #84a7db; border-bottom: 3px solid #84a7db; padding-left: 1em; font-weight: bold;"> |
Notice | Notice | ||
</div> | </div> | ||
− | < | + | <syntaxhighlight lang="php"> |
JError::raiseNotice( 100, 'Notice' ); | JError::raiseNotice( 100, 'Notice' ); | ||
− | </ | + | </syntaxhighlight> |
− | == Warning == | + | <translate> |
+ | == Warning == <!--T:5--> | ||
+ | </translate> | ||
− | <div style="background-color: # | + | <div style="background-color: #efe7b8; color: #c00; border-top: 3px solid #f0dc7e; border-bottom: 3px solid #f0dc7e; padding-left: 1em; font-weight: bold;"> |
Warning | Warning | ||
</div> | </div> | ||
− | < | + | <syntaxhighlight lang="php"> |
JError::raiseWarning( 100, 'Warning' ); | JError::raiseWarning( 100, 'Warning' ); | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | <translate> | ||
+ | == Error == <!--T:6--> | ||
+ | </translate> | ||
− | = | + | <div style="background-color: #e6c0c0; color: #c00; border-top: 3px solid #DE7A7B; border-bottom: 3px solid #DE7A7B; padding-left: 1em; font-weight: bold;"> |
− | < | + | Error |
+ | </div> | ||
+ | <syntaxhighlight lang="php"> | ||
JError::raiseError( 4711, 'A severe error occurred' ); | JError::raiseError( 4711, 'A severe error occurred' ); | ||
− | </ | + | </syntaxhighlight> |
− | == Joomla! 3.x | + | <translate> |
− | Since Joomla! {{JVer|3. | + | == Joomla! 3.x is Bootstrapped == <!--T:7--> |
+ | </translate> | ||
+ | <translate><!--T:8--> | ||
+ | Since Joomla! {{JVer|3.x}} uses Bootstrapped templates, the messages will use the standard Bootstrap CSS styles for Alerts.</translate> | ||
− | See: | + | <translate><!--T:9--> |
+ | See: https://getbootstrap.com/2.3.2/components.html#alerts</translate> | ||
− | The general syntax remains: | + | <translate><!--T:10--> |
+ | The general syntax remains:</translate> | ||
− | < | + | <syntaxhighlight lang="php"> |
− | JFactory::getApplication()->enqueueMessage('Your Message', 'type); | + | JFactory::getApplication()->enqueueMessage('Your Message', 'type'); |
− | </ | + | </syntaxhighlight> |
− | Where | + | <translate><!--T:11--> |
+ | Where ''type'' can be one of</translate> | ||
− | * | + | <translate><!--T:12--> |
− | * ' | + | * 'message' (or empty) - green |
+ | * 'notice' - blue | ||
+ | * 'warning' - yellow | ||
* 'error' - red | * 'error' - red | ||
− | + | </translate> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | <noinclude>[[Category:Development]] | + | <translate> |
− | [[Category:Tutorials]][[Category:Component Development]] | + | == See Also == <!--T:13--> |
+ | </translate> | ||
+ | <translate><!--T:14--> | ||
+ | * [https://api.joomla.org/cms-3/classes/JError.html JError on api.joomla.org] | ||
+ | * [https://api.joomla.org/cms-3/classes/JApplication.html JApplication on api.joomla.org] | ||
+ | </translate> | ||
+ | __NOTOC__ | ||
+ | <noinclude> | ||
+ | <translate> | ||
+ | <!--T:15--> | ||
+ | [[Category:Development]] | ||
+ | [[Category:Tutorials]] | ||
+ | [[Category:Component Development]] | ||
+ | </translate> | ||
+ | </noinclude> |
Latest revision as of 18:00, 29 November 2022
Errors, warnings, alerts and notices can be displayed from any component, module, plugin or template using the methods outlined below.
// Get a handle to the Joomla! application object
$application = JFactory::getApplication();
// Add a message to the message queue
$application->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
/** Alternatively you may use chaining */
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
The second argument to the enqueueMessage function is the type of the message. The default is message, but error results in a different style for the message. The message will be displayed in place of a special jdoc:include statement in your template. Place the following in your template at the location where you want messages to appear.
<jdoc:include type="message" />
Message[edit]
Message
JFactory::getApplication()->enqueueMessage('Message');
Notice[edit]
Notice
JError::raiseNotice( 100, 'Notice' );
Warning[edit]
Warning
JError::raiseWarning( 100, 'Warning' );
Error[edit]
Error
JError::raiseError( 4711, 'A severe error occurred' );
Joomla! 3.x is Bootstrapped[edit]
Since Joomla! uses Bootstrapped templates, the messages will use the standard Bootstrap CSS styles for Alerts.
See: https://getbootstrap.com/2.3.2/components.html#alerts
The general syntax remains:
JFactory::getApplication()->enqueueMessage('Your Message', 'type');
Where type can be one of
- 'message' (or empty) - green
- 'notice' - blue
- 'warning' - yellow
- 'error' - red