The hook works fine and quicktesting it by changing the page title it worked like a charm. Digging deeper into it I ran into another problem:
It seems that there is a timing problem with the repeater on page > add new.
If I create a new subpage, give it a title and hit save the url switches from "/admin/page/add/?parent_id=1" to "/admin/page/edit/?id=1234&new=1". now I am able to see all fields including the repeater. My module changes the title correctly but has no influence to the repeater fields. if I reload the page or click on edit in the page tree it works like expected but not on the first initial form rendering.
Here is my current module code:
<?php namespace ProcessWire;
class SetDefaultValues extends Process implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Set Default Values',
'version' => 1,
'summary' => __('Set default values on page add / edit'),
'singular' => true,
'autoload' => true,
);
}
public function init() {
$this->addHookAfter('ProcessPageEdit::loadPage', $this, 'setDefaultValues');
}
public function setDefaultValues(HookEvent $event) {
$page = $event->return;
if ($page->template == 'my_template') {
$defaults = array(
'Default Headline A',
'Default Headline B',
'Default Headline C',
);
$page->title = "Default Title"; // this works
if ($page->my_repeater) {
foreach ($page->my_repeater as $i => $block) {
$block->headline = empty($block->headline) ? $defaults[$i] : $block->headline; // only works on edit, not on add
}
}
}
}
}
Any ideas how to solve this problem?