The onBeforeOutput handler works very similarly to the onOutput handler, except that it is run before widgets and XPHP variables have been processed (i.e., they still appear as <widget id="...
or <xphp var="...
in the $buffer).
Furthermore, as the below example indicates, REGISTERED_BODY_CLASSES
haven’t yet been added to the page HTML, so you can still edit that array.
<?php
$_LW->REGISTERED_APPS['my_app']=[
'title'=>'My App',
'handlers'=>['onBeforeOutput'],
];
class LiveWhaleApplicationMyApp {
public function onBeforeOutput($buffer) {
global $_LW;
// add page_id_123 to the body class
$_LW->applyPageAndGroupVars('<xphp var="page_id"/>');
if (!empty($GLOBALS['page_id'])) {
$_LW->REGISTERED_BODY_CLASSES[]='page_id_'.$GLOBALS['page_id'];
};
return $buffer;
}
}
?>
A common use of onBeforeOutput is to create custom XPHP variables that you want to use in your pages or templates.
For instance, the below /client/modules/list_all_calendars/public.application.list_all_calendars.php can be used to replace <xphp var=”all_calendars”/> with a list of all calendar groups with feed links:
<?php
$_LW->REGISTERED_APPS['list_all_calendars']=array(
'title'=>'List All Calendars',
'handlers'=>array('onBeforeOutput'),
'application'=>array(
'order'=>-1
)
); // configure this module
class LiveWhaleApplicationListAllCalendars {
public function onBeforeOutput($buffer) {
global $_LW;
if (strpos($buffer, '<xphp var="all_calendars"')!==false) { // // Populate the group calendar variable if it's used
$GLOBALS['all_calendars']='<div id="lw_all_calendars">';
foreach($_LW->dbo->query('select', 'livewhale_groups.fullname as title, IF(livewhale_groups.fullname_public IS NOT NULL, livewhale_groups.fullname_public, livewhale_groups.fullname) as display_title, livewhale_groups.directory', 'livewhale_groups', 'livewhale_groups.directory IS NOT NULL AND livewhale_groups.fullname!="Public"', 'IF(livewhale_groups.fullname_public IS NOT NULL, livewhale_groups.fullname_public, livewhale_groups.fullname) ASC')->groupBy('livewhale_groups.id')->run() as $res2) { // fetch calendars
$GLOBALS['all_calendars'].='<h3 class="calendar_group">' . $_LW->setFormatClean($res2['display_title']) . '</h3>
<ul class="feed_source">
<li><a href="' . $_LW->setFormatClean($res2['directory']) . '">Link</a></li>
<li><a href="/live/ical/events/group/' . $_LW->setFormatClean($res2['title']) . '">iCal</a></li>
<li><a href="/live/rss/events/group/' . $_LW->setFormatClean($res2['title']) . '">RSS</a></li>
<li><a href="/live/json/events/group/' . $_LW->setFormatClean($res2['title']) . '">JSON</a></li>
</ul>';
};
$GLOBALS['all_calendars'].='</div>';
}
return $buffer;
}
}
?>
Or, you can use a simple PHP “for” loop to generate a list of years for a form dropdown:
<?php
$_LW->REGISTERED_APPS['my_app']=[
'title'=>'My App',
'handlers'=>['onBeforeOutput'],
];
class LiveWhaleApplicationMyApp {
public function onBeforeOutput($buffer) {
global $_LW;
if (strpos($buffer, '<xphp var="years_dropdown"')!==false) { // // Populate <xphp var="years_dropdown"/> if it's used
$GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
for ($i = 2010; $i <= date('Y'); $i++) { // include years from 2010 to the present
$GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
}
$GLOBALS['years_dropdown'].='</select>';
}
return $buffer;
}
}
?>