Basically, this allows you to mirror the admin form from a given page to the frontend.
To give some flexibility, you can fill an array with the fields to exclude —or else, the fields to include— on the output.
EDIT: I forgot to tell... this works well with regular fields (text, textareas, checkboxes, radios, multiple choice, etc), but didn't manage to make it work well with images, for instance.
So, here is the code:
<?php
// Get the page you need to edit
$mypage = $pages->get('/some/page/');
// Populate with the names of the fields you want to exclude OR include (see instructions below)
// Leave empty to output all the fields
$myfields = array('body', 'email');
$form = $modules->get('InputfieldForm');
$fields = $mypage->getInputfields();
// If array is not empty use it to filter the fields
if ($myfields){
foreach($fields as $f){
// Output all the fields minus the ones listed in the $myfields array
// Instead, to output only the fields that are in the array, remove the (!) from the condition
if (!in_array($f->name, $myfields)){
$form->append($f);
}
}
}
// Else, include all the fields
else {
$form->append($fields);
}
// Add save button
$field = $this->modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_save');
$field->attr('value', 'Save');
$field->label = "submit herei";
$form->append($field);
// Process the form
// (code replaced by a new one provided by Ryan)
if($input->post->submit_save) {
$form->processInput($input->post);
if(!$form->getErrors()) {
$mypage->of(false); // turn off output formatting before setting values
foreach($mypage->fields as $f) {
$mypage->set($f->name, $form->get($f->name)->value);
}
}
}
include("./head.inc");
// Render the form
echo $form->render();
include("./foot.inc");
Edited by diogo, 19 May 2012 - 02:19 PM.













