Skip to content

Instantly share code, notes, and snippets.

@niklaka
Created January 5, 2013 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklaka/4462130 to your computer and use it in GitHub Desktop.
Save niklaka/4462130 to your computer and use it in GitHub Desktop.
An example on hooking Pages::saveReady to write a hidden value upon page save. Based on HelloWorld.module from ProcessWire default installation, only slight modifications.
<?php
/**
* ProcessWire 'Hello world' demonstration module
*
* Demonstrates the Module interface and how to add hooks.
*
* 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 Helloworld 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(
// The module'ss title, typically a little more descriptive than the class name
'title' => 'Hello World',
// version: major, minor, revision, i.e. 100 = 1.0.0
'version' => 101,
// summary is brief description of what this module is
'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.',
// Optional URL to more information about the module
'href' => 'http://www.processwire.com',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'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() {
// add a hook after the $pages->saveReady
$this->pages->addHookAfter('saveReady', $this, 'example1');
}
/**
* Example1 hooks into the pages->saveReady and writes a value to a hidden field
*
*/
public function example1($event) {
// the saved page is given as an argument to the saveReady() method
// and the first argument is retrieved from the event like this
$page = $event->arguments[0];
// do nothing if the page saved has some other template than the one we're aiming for
if($page->template != 'box-template') return;
// calculate a value for the hidden field based on some fields given by the user
$page->hiddenBoxArea = (int)$page->boxWidth * (int)$page->boxHeight;
$this->message("Hello World! You saved {$page->path} with an area of " . $page->hiddenBoxArea);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment