Skip to content

Instantly share code, notes, and snippets.

@ocorreiododiogo
Created November 12, 2013 08:54
Show Gist options
  • Save ocorreiododiogo/7427713 to your computer and use it in GitHub Desktop.
Save ocorreiododiogo/7427713 to your computer and use it in GitHub Desktop.
For Processwire. Mirror admin forms in the frontend. Works well with regular fields (text, textareas, checkboxes, radios, multiple choice, etc), but didn't manage to make it work well with images
<?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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment