suntrop Posted February 27, 2017 Share Posted February 27, 2017 Hi all. I have to set default values for some fields (page select and text fields) on my page. I think the /site/templates/admin.php is the appropriate place? But I can't get anything to work from there. That is my current code, that looks to me like it should work. But it doesn't wire()->addHookAfter('Page::added', function($event) { wire('page')->setAndSave('page_select_field', 1042); wire('page')->setAndSave('text_field', 'Default Value'); }); require($config->paths->adminTemplates . 'controller.php'); Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 27, 2017 Share Posted February 27, 2017 It depends. The admin.php is only executed if you visit the admin backend. If you add pages as part of any other page in your website the hook would need to be in site/ready.php. Also your hook was not correct. There's no Page::added hook (nor function), and in the Pages::added you can retrieve the saved page from the functions arguments. $this->wire()->addHookAfter('Pages::added', function($event) { $page = $event->arguments(0); if($page->template != 'xxx') return; $page->setAndSave([ 'page_select_field' => 1042, 'text_field' => 'Default Value' ]); }); 2 Link to comment Share on other sites More sharing options...
suntrop Posted February 27, 2017 Author Share Posted February 27, 2017 Thanks for your quick response! I looked in Captain Hook and saw … #1280: public function ___added(Page $page) … but didn't noticed it was in the Pages section. Had just seen Page $page. Anyhow, the code above works great. I only need it for pages created in the CMS backend. 17 minutes ago, LostKobrakai said: if you add pages as part of any other page I guess you mean front-end pages (template), right? Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 27, 2017 Share Posted February 27, 2017 12 minutes ago, suntrop said: I guess you mean front-end pages (template), right? Yeah. Link to comment Share on other sites More sharing options...
uzlander Posted December 17, 2023 Share Posted December 17, 2023 Hi there! I've been looking for some similar solutions, but my case with the admin section setting/overriding default values, (need to setAndSave ''disable_automatic_append_of_file:_main.php' => true', 'prepend_file' => 'site-globals.php'), doing above modification in templates/admin.php, it seems like i'm missing something or it just doesn't reflect in the admin corresponding tabs. Please somebody correct me Link to comment Share on other sites More sharing options...
da² Posted December 17, 2023 Share Posted December 17, 2023 @uzlander I'd use saveReady and isNew() to initialize a default value, so the hook only apply the first time page is created: $this->addHookBefore('Pages::saveReady(template=my-template)', function (HookEvent $event): void { $page = $event->arguments(0); if ($page->isNew()) { $page->myField = "myDefaultValue"; } }); Or like this, replacing isNew() by id=0 in hook selector: $this->addHookBefore('Pages::saveReady(template=my-template, id=0)', function (HookEvent $event): void { $page = $event->arguments(0); $page->myField = "myDefaultValue"; }); Link to comment Share on other sites More sharing options...
uzlander Posted January 16 Share Posted January 16 Oh, thank you so much for elaborating && kind reply )). Am about to test your solution above #da2#, i believe it should work though.. I've been out for a while and now having an issue accessing an OS(zorin glitch), so i'm gonna start over with fresh installation and work it through. Thanks a lot !:) 1 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