Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created April 23, 2014 08:18
Show Gist options
  • Save somatonic/11206761 to your computer and use it in GitHub Desktop.
Save somatonic/11206761 to your computer and use it in GitHub Desktop.
add new save button on page edit, to do something additional on save
<?php
/**
* Adding other types of save buttons for page edit form.
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class CustomPageSave extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'CustomPageSave',
'version' => 1,
'summary' => 'Example for adding other save buttons to page edit',
'href' => 'http://www.processwire.com',
'singular' => true,
'autoload' => true,
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
$this->addHookAfter("ProcessPageEdit::buildForm", $this, "addSaveButton");
// tell processwire that this is a page save
if($this->input->post->submit_save_minor){
$this->input->post->submit_save = 1;
// attach hook on page save
$this->addHookAfter("Pages::saveReady", $this, "hookPageSave");
}
}
public function hookPageSave($event){
$page = $event->arguments("page");
if($this->input->post->submit_save_minor){
// increment an integer field "minor" or do something else
// this will get saved after this saveReady hook so no need to save here
$page->minor = $page->minor + 1;
$this->message("Saved minor");
}
}
public function addSaveButton($event){
$form = $event->return;
// new submit button
$f = $this->modules->InputfieldSubmit;
$f->attr("name", "submit_save_minor");
$f->attr("value", "Save minor");
// add submit button after the regular save button
$form->insertAfter($f, $form->get("submit_save"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment