Version two would then be
<?php
class SaveShortUrl extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Rewrite field after save',
'version' => 2,
'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.',
'href' => 'http://www.processwire.com',
'singular' => true,
'autoload' => 'template=admin',
'icon' => 'smile-o',
);
}
public function init() {
/**
* Hook called just before a page is saved
*
* May be preferable to a before(save) hook because you know for sure a save will
* be executed immediately after this is called. Whereas you don't necessarily know
* that when before(save) is called, as an error may prevent it.
*/
$this->pages->addHookAfter('saveReady', $this, 'hookPageSave');
}
/**
* Save domain info to another field
* No need to call page->save() when changing a value
* as it will get saved just after this call.
*
* Make sure there's something in source_url and it has actually changed
* with $page->isChanged(what)
*/
public function hookPageSave(HookEvent $event) {
$page = $event->arguments("page");
if($page->template != "basic-page") return;
if(!$page->source_url) return;
if(!$page->isChanged("source_url")) return;
$page->domain = parse_url($page->source_url, PHP_URL_HOST);
if($page->domain) $this->message("Saved the domain: {$page->domain}.");
}
}
now you have already a module in the "modules" table in your DB, go to phpmyadmin and remove the entry for "saveShortUrl"
Come back and refresh modules, and install the new one.