Skip to content

Instantly share code, notes, and snippets.

@karlhinze
Last active December 19, 2017 20:09
Show Gist options
  • Save karlhinze/a7147fe41f5a7de74c8c31fa572a23d1 to your computer and use it in GitHub Desktop.
Save karlhinze/a7147fe41f5a7de74c8c31fa572a23d1 to your computer and use it in GitHub Desktop.
This new-and-improved LiveWhale module uses five handlers to get and set a custom field on an existing LiveWhale datatype.
<?php
/*
This is a sample application designed to demonstrate how to add an additional custom field to a backend editor.
The example provided adds a "Dress Code" field to the existing event editor in LiveWhale. To add a custom field to the news editor, for example, follow the same example but change all references from events to news below.
*/
$_LW->REGISTERED_APPS['custom_fields'] = array( // configure this application module
'title' => 'Custom Fields',
'handlers' => array('onLoad', 'onAfterValidate', 'onSaveSuccess', 'onAfterEdit', 'onOutput')
);
class LiveWhaleApplicationCustomFields {
/* The onLoad() handler allows you to load in additional resources for the page when this application first loads. */
public function onLoad() {
global $_LW;
if ($_LW->page=='events_edit') { // if on the events editor page
//$_LW->REGISTERED_CSS[]='/path/to/custom/stylesheet.css'; // load in some custom CSS for styling the new field (optional).
$_LW->REGISTERED_CSS[]=$_LW->CONFIG['LIVE_URL'].'/resource/css/mymodule%5Cmymodule.css'; // load in some CSS for styling the new field from a custom module (optional)
$_LW->ENV->input_filter['events_edit']['dress_code'] = array('tags' => '*', 'wysiwyg' => 1); // configure the input filter to present the custom field as a WYSIWYG field (omit this line entirely for no HTML allowed, or change "wysiwyg" to "wysiwyg_limited" for the limited set of toolbar options)
};
}
/* The onAfterValidate() handler allows you to add additional validation checks after clicking the save button on a backend editor. */
public function onAfterValidate() {
global $_LW;
if ($_LW->page == 'events_edit') { // if saving from the events editor page
if (!empty($_LW->_POST['dress_code']) && stripos($_LW->_POST['dress_code'], 'supercalifragilisticexpialidocious') !== false) { // disallow the word "supercalifragilisticexpialidocious" from the custom field
$_LW->REGISTERED_MESSAGES['failure'][] = 'The custom field cannot contain the word supercalifragilisticexpialidocious.'; // register error
};
};
}
/* The onAfterEdit() handler allows you to load additional custom data from the database after the default editor data is loaded in. */
public function onAfterEdit($type, $page, $id) {
global $_LW;
if ($page == 'events_edit') { // if loading data for the events editor form
if (empty($_LW->_POST['dress_code'])) { // if loading the editor for the first time (as opposed to a failed submission)
if (!empty($id)) { // and loading a previously saved event
if ($fields = $_LW->getCustomFields($type, $id)) { // getCustomFields($type, $id) gets any previously saved custom data for the item of this $type and $id
foreach($fields as $key => $val) { // add previously saved data to POST data so it prepopulates in the editor form
$_LW->_POST[$key] = $val;
};
};
};
};
};
}
/* The onSaveSuccess() handler allows you to store the custom data after the event first saves its default set of data. */
public function onSaveSuccess($type, $id) {
global $_LW;
if ($type == 'events') { // if saving an event
$_LW->setCustomFields($type, $id, array('dress_code' => @$_LW->_POST['dress_code']), array()); // store the value entered for dress_code, allowing the dress_code field full visibility (on details pages, in widget results, and /live/* requests such as /live/json)
/*
To optionally hide the field (i.e. store it in the database but not expose it to the public on the frontend web site or API requests, add "dress_code" to the empty array above).
If not hiding the custom field, it may be added to an events template via <xphp var="events_custom_dress_code"/> or to a widget result via {custom_dress_code}.
*/
};
}
/* The onOutput() hander allows you to add the custom form element to the editor. */
public function onOutput($buffer) {
global $_LW;
if ($_LW->page == 'events_edit') { // if on the events editor page
// You can use displayCustomField to display many field types:
// $_LW->displayCustomField('text', 'myfield', @$_LW->_POST['myfield'], false)
// $_LW->displayCustomField('textarea', 'myfield', @$_LW->_POST['myfield'], false)
// $_LW->displayCustomField('select', 'myfield', @$_LW->_POST['myfield'], array('Select 1', 'Select 2', 'Select 3'))
// $_LW->displayCustomField('checkbox', 'myfield', @$_LW->_POST['myfield'], array('Checkbox 1', 'Checkbox 2', 'Checkbox 3'))
// $_LW->displayCustomField('radio', 'myfield', @$_LW->_POST['myfield'], array('Radio 1', 'Radio 2', 'Radio 3'))
//
// Prepend the dress_code element to the existing tags field (or any other position you like in the page) -- and preload any existing value we have.
$buffer = str_replace(
'<!-- END LOCATION -->',
'<!-- END LOCATION -->
<div class="fields fields_dress_code">
<label class="header" for="dress_code">Dress Code</label>
<fieldset>
' . $_LW->displayCustomField('text', 'dress_code', @$_LW->_POST['dress_code'], false) . '
<div class="note">Enter the dress code for your event (optional).</div>
</fieldset>
</div>',
$buffer
);
};
return $buffer;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment