Tobi C. Posted January 16, 2019 Share Posted January 16, 2019 Hi, I am a little stuck, while trying to set some default headlines for repeater fields when an user creates a new page of a specific template. I think this has to be done with hooks but I dont know which hook to use for that case. The repeater creates a minimum of 4 instances and each contains of a headline and a textarea. Maybe anyone here can help. Thanks. Link to comment Share on other sites More sharing options...
flydev Posted January 16, 2019 Share Posted January 16, 2019 Hi @Tobi C. have a read to those threads : 1 Link to comment Share on other sites More sharing options...
Tobi C. Posted January 16, 2019 Author Share Posted January 16, 2019 Hi @flydev thanks for your help. The first thread was exactly what I was searching for. $this->addHookAfter('ProcessPageEdit::loadPage', $this, 'onProcessPageEdit'); That did the job just fine. 1 Link to comment Share on other sites More sharing options...
BitPoet Posted January 16, 2019 Share Posted January 16, 2019 1 minute ago, Tobi C. said: That did the job just fine. You could experiment with Pages::added too. This would also run the code when you add a new page through the API. 1 Link to comment Share on other sites More sharing options...
Tobi C. Posted January 16, 2019 Author Share Posted January 16, 2019 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? Link to comment Share on other sites More sharing options...
horst Posted January 16, 2019 Share Posted January 16, 2019 You may additionally try out the hook "setupNew" $this->addHookAfter('Pages::setupNew', $this, 'setDefaults'); $this->addHookAfter('Pages::added', $this, 'setDefaults'); or as @BitPoet already mentioned "Pages::added". Link to comment Share on other sites More sharing options...
Tobi C. Posted January 16, 2019 Author Share Posted January 16, 2019 Thanx, @horst, I already tried this and some other hooks but they didn't change anythings. I dont think that the hook is the problem as setting the title works. Link to comment Share on other sites More sharing options...
Tobi C. Posted January 20, 2019 Author Share Posted January 20, 2019 It was a timing problem. I tried to set headlines of repeater elements that didn't exist on the first ProcessPageEdit::loadPage. The solution was to set min repeaters back to 0 and create the needed repeaters by api on Pages::added. <?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 new pages'), 'singular' => true, 'autoload' => true, ); } public function init() { $this->addHookAfter('Pages::added', $this, 'createDefaultRepeaterFields'); } public function createDefaultRepeaterFields(HookEvent $event) { $page = $event->arguments(0); if ($page->template == 'my_template') { if (count($page->my_repeater) < 1) { $defaults = array( 'Headline A', 'Headline B', 'Headline n', ); foreach ($defaults as $default) { $r = $page->my_repeater->getNew(); $r->headline = $default; $r->save(); $page->my_repeater->add($r); } $page->save(); } } } } Thanks for helping with this. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now