Created
April 2, 2014 00:20
-
-
Save mckelvey/9925748 to your computer and use it in GitHub Desktop.
This Alex-made™ code demonstrates how to use five handlers to get and set a custom field on an existing LiveWhale datatype.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
}; | |
} | |
/* 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 | |
$_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) | |
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 | |
$buffer = str_replace( // prepend the dress_code element to the existing places field (or any other position you like in the page) -- and preload any existing value we have | |
'<div id="fields_places">', | |
'<fieldset class="textarea" id="events_dress_code_fieldset"> | |
<label for="events_dress_code">Dress Code</label> | |
<textarea name="dress_code" id="events_dress_code" cols="40" rows="3" class="dress_code">'.@$_LW->_POST['dress_code'].'</textarea> | |
</fieldset> | |
<div id="fields_places">', | |
$buffer | |
); | |
}; | |
return $buffer; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment