Hi there, i've been trying to create a module which works in the admin panel, and does the following:
Auto-fill the "summary" field with some body excerpt in case the user leaves it blank.
Can't figure what is wrong and it refuses to function as i expect. So, the code:
<?php namespace ProcessWire;
class FillSummaryHook extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Fill Summary Field',
'summary' => 'Useful module',
'version' => 0.4,
'autoload' => true,
);
}
public function ready() {
$this->pages->addHookBefore('saved', function($event) {
$page = $event->arguments[0];
$summaryField = $page->field('summary');
$bodyField = $page->field('body');
// If the summary field is empty, set it to the body excerpt
if (empty($summaryField->value)) {
$summaryField->value = strip_tags(substr($bodyField->value, 0, 192));
}
});
}
}
The file's named: "FillSummaryHook.module" which is inside the same named folder within "modules" directory.
On saving the page it outputs (screenshot)