cb2004 Posted April 13, 2016 Share Posted April 13, 2016 I am having a bit of a brain ache with this one. Can anybody point me in the right direction. At the moment I just want to get the field text_1 populated with the value test and then I should be good to go. Cheers. <?php $pages->addHookAfter("added", function($event) { $page = $event->object; if($page->template->id == 46) { $page->text_1 = "test"; } }); Link to comment Share on other sites More sharing options...
BitPoet Posted April 13, 2016 Share Posted April 13, 2016 You need to save your change. For PW < 2.7: $of = $page->of(); $page->of(false); $page->set('text_1', 'test'); $page->save('text_1'); $page->of($of); Or for PW >= 2.7: $page->setAndSave("text_1", "test"); 1 Link to comment Share on other sites More sharing options...
cb2004 Posted April 13, 2016 Author Share Posted April 13, 2016 Cheers BitPoet, but there is an error after adding: Method Pages::setAndSave does not exist or is not callable in this context Link to comment Share on other sites More sharing options...
cb2004 Posted April 13, 2016 Author Share Posted April 13, 2016 This is in the ready.php file, I think it would work with no problems on the frontend. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 13, 2016 Share Posted April 13, 2016 $event->object will always give you the object the hook function is on. In this case it's pages. $pages->addHookAfter("added", function($event) { // $pages = $event->object; $page = $event->arguments(0); if($page->template->id == 46) { $page->setAndSave('text_1', "test"); } }); Link to comment Share on other sites More sharing options...
cb2004 Posted April 13, 2016 Author Share Posted April 13, 2016 Cheers LostKobrakai. Still no joy with this though. Link to comment Share on other sites More sharing options...
cb2004 Posted April 13, 2016 Author Share Posted April 13, 2016 I tacked on $this->message("WORKING"); just to make sure I got this message and I did. Link to comment Share on other sites More sharing options...
cb2004 Posted April 13, 2016 Author Share Posted April 13, 2016 (edited) I have a working solution. I am taking a guess that it cannot update a field directly in the page you are saving without a bit of extra information: <?php $pages->addHookBefore('added', function($event) { $page = $event->arguments(0); if ($page->template->id == 46) { $p = wire('pages')->get($page->id); $p->setAndSave("text_1", "test"); } }); The solution by LostKobrakai works as well. Edited April 14, 2016 by cb2004 1 Link to comment Share on other sites More sharing options...
bernhard Posted April 13, 2016 Share Posted April 13, 2016 That looks strange... Does anybody know why lostkobrakai's solution does not work? 1 Link to comment Share on other sites More sharing options...
cb2004 Posted April 14, 2016 Author Share Posted April 14, 2016 I could be wrong but I think LostKobrakai edited his post from $page->text_1 = "test"; to $page->setAndSave('text_1', "test"); as I revisited his solution and it worked fine. 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