Skip to content

Instantly share code, notes, and snippets.

Last active June 26, 2018 19:23
  • Star 11 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Create a page edit form on frontend using PW API and fields from the template of the page
// get a page
$editpage = $pages->get("/editme/");
$ignorefields = array("isOld","language_published");
$form = $modules->get("InputfieldForm");
$form->method = 'post';
$form->action = './';
// get the collection of inputs that can populate this page's fields
$inputfields = $editpage->getInputfields();
// make all the fields required and add them to the form
foreach($inputfields as $inputfield) {
if(in_array($inputfield->name, $ignorefields)) continue;
// the inputfields don't already have a submit button, so we'll add one.
$submit = $modules->get("InputfieldSubmit");
$submit->name = "submit";
$submit->value = 'Submit';
// add the submit button the the form
$out = '';
// process the form
if($this->input->post->submit) {
// now we assume the form has been submitted.
// tell the form to process input frmo the post vars.
// see if any errors occurred
if( count( $form->getErrors() )) {
// re-render the form, it will include the error messages
$out .= $form->render();
} else {
// successful form submission, so populate the page with the new values.
foreach($form as $field) {
$editpage->set($field->name, $field->value);
// save it
$out .= "Page saved.";
$out .= $form->render();
} else {
$out .= $form->render();
include(""); // where scripts are added needed by the form
echo $out;
$jsConfig = $config->js();
$jsConfig['urls'] = array(
'root' => $config->urls->root,
'admin' => $config->urls->admin,
'modules' => $config->urls->modules,
'core' => $config->urls->core,
'files' => $config->urls->files,
'templates' => $config->urls->templates,
'adminTemplates' => $config->urls->adminTemplates,
'adminTemplates' => $config->urls->adminTemplates,
var config = <?php echo json_encode($jsConfig); ?>;
$config->styles->prepend($config->urls->adminTemplates . "styles/main.css?v=2");
$config->styles->append($config->urls->adminTemplates . "styles/inputfields.css");
$config->styles->append($config->urls->adminTemplates . "styles/ui.css?v=2");
$config->scripts->append($config->urls->JqueryUI . "JqueryUI.js");
$config->scripts->prepend($config->urls->JqueryCore . "JqueryCore.js");
$config->scripts->append($config->urls->adminTemplates . "scripts/inputfields.js");
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";
foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
Copy link

Weird mix of js + php in head.php. Can you do that?

Shorthand <? instead of <?php can cause problems.

Copy link

@modifiedcontent ye you can it's nothing weird. The shorthand wasn't intentional (edited).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment