Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Passing an error to admin message bar
How does one pass an error message from a plugin so that it appears in the admin message/announce box?
I have:
$msg = array( gTxt('text_pack_error_message', array('{path}' => $curDir)), E_ERROR);and at the top I have
pagetop(gTxt('text_pack_page_head'), $msg);but how do I pass $msg back to the page reload?
TXP Builders – finely-crafted code, design and txp
Offline
#2 2016-06-16 21:40:33
- GugUser
- Member
 
- From: Quito (Ecuador)
- Registered: 2007-12-16
- Posts: 1,477
Re: Passing an error to admin message bar
A related post without response.
Offline
Re: Passing an error to admin message bar
jakob wrote #299790:
how do I pass
$msgback to the page reload?
Normally it’s in the function signature of your main function that draws the interface. Here’s a skeleton plugin that demonstrates the idea.
register_callback('jcr_dispatcher', 'your_event');
...
/**
 * Jump off to relevant stub for handling actions.
 */
function jcr_dispatcher($evt, $stp)
{
   // true means a valid txp_token must be present or the action will fail.
   $available_steps = array(
      'jcr_store_info_ui'  => true,
      'jcr_update_info_ui' => true,
      ...
   );
   // Use default step if nothing matches.
   if (!$stp or !bouncer($stp, $available_steps)) {
      $stp = 'jcr_main';
   }
   // Run the function.
   $stp();
}
/**
 * Draw the main interface.
 *
 * Note the message is (optionally) passed in.
 */
function jcr_main($msg = '')
{
   pagetop(gTxt('textpack_page_head'), $msg);
   // ... draw UI here
}
/**
 * Stash new row in the database.
 */
function jcr_store_info()
{
   // Read values passed in from POST...
   $val = ps('your_value');
   ...
   // ... and store them...
   $ret = safe_insert('table', "column='" . doSlash($val) . "'");
   ...
   return $ret;
}
/**
 * Stub to handle saving and return to UI.
 */
function jcr_store_info_ui()
{
   // Call the function that does the work.
   $result = jcr_store_info();
   // Test the result and set the message accordingly.
   if ($result) {
      $msg = 'yay';
   } else {
      $msg = 'nope';
   }
   // Call the main function to redraw the UI and pass the message in.
   jcr_main($msg);
}That’s the ‘functional’ approach. A better method is a class-based approach (I’m gradually porting my admin code over to that) but we’ll save that for another day.
The reason the save function is split into two, btw — one for the UI that is called from the plugin’s dispatcher, and one to do that actual job — is so that we, or other plugins, can reuse the “just do the job” function independently of your UI. Core should use this methodology, but doesn’t — at least not all the time: it’s on the todo list as we get round to refactoring the admin panels.
Hope that helps, but holler if anything’s unclear.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Passing an error to admin message bar
Thanks. That was a great help. I rejigged things and have it nearly working – only I’m getting a second set of txp menus nested within the original txp menu block (this is on a v4.5.7 txp with the “classic” theme). Obviously I need to effect a proper page reload?
EDIT: Maybe it’s not so important as it doesn’t happen with Hive on 4.6.
TXP Builders – finely-crafted code, design and txp
Offline

