Hi there,
Was just wondering if there is an easy way to provide an error message from within a plugin?
Say for instance I have the following :
{exp:my_plugin plugin_parameter="name"}
{name}
{/exp:my_plugin}
Let’s just say that whatever the site creator places as the plugin_parameter=”name” is not valid then what do you do to let them know?
If this was say querying the database and bringing back a result or in this case not bringing back a result then would you even provide an error message as that would stop the use of the template wouldn’t it?
Just wondering what the best way to go about it all is?
Thanks.
Best wishes,
Mark
Also meant to ask one other question on this.
Let’s say I had this plugin code :
{exp:my_plugin plugin_parameter="{member_id}"}
{name}
{/exp:my_plugin}
If I want to use the {member_id} global variable as the value for the plugin parameter then when I do this in a plugin :
$member_id = $TMPL->fetch_param('member_id');
I don’t get the member_id returned if I try to echo out the value passed, instead I just get {member_id} exactly like that with the braces spat out.
I know I can do this :
$member_id = $SESS->userdata['member_id'];
but I don’t want to, I need to be able to use global variables instead. Is this not possible?
Best wishes,
Mark
Hi Mark,
Was just wondering if there is an easy way to provide an error message from within a plugin?
May be that isn’t correct, but I provide error messages in my plugins using simple PHP echo statements, as here:
echo 'ERROR! The value of parameter "name" of exp:my_plugin tag is invalid.';
Such message gets printed on top of the screen - exactly as needed.
Hiya,
Yep I know about echo but as far as I know that indeed isn’t the right way to do things. I’m sure that there is a function which will put a box on the screen with the error in it. Not too big of a deal though because if someone has made an error in a tag anyway then any error is better than none I suppose 😉
Best wishes,
Mark
Hi Mark,
Yep I know about echo but as far as I know that indeed isn’t the right way to do things. I’m sure that there is a function which will put a box on the screen with the error in it.
Is it really echo wrong way to display error message? General Style and Syntax guidelines does not say anything about how error messages should be displayed.
Box on the screen with a message is not much better than error message displayed using echo statement. Echo statement even seems to be better since when error is displayed in a box the rest of the page isn’t rendered; and echo statement does not prevent the rest of the page being rendered.
Hey Mark,
try this for error output:
global $OUT, $LANG;
$LANG->fetch_language_file('my_plugin');
if ($error)
{
return $OUT->show_user_error('general', $LANG->line('my_error_message'));
}
Of course, you’ll need to have lang.my_plugin.php in your language files and string corresponding to ‘my_error_message’ in it.
BTW, did you find any way to pass {member_id} as plugin parameter??
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.