VeiJari Posted April 11, 2019 Posted April 11, 2019 Hello forum! I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates) for a certain template. Here's the code: Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. But the code isn't doing anything. Not even dumping anything What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? ? Thanks for the support in advance!
psy Posted April 11, 2019 Posted April 11, 2019 Hi @VeiJari It's usual practice, but not compulsory, to put hooks into /site/ready.php. site/ready.php loads before _init.php. You're changing the saved page data so you may need to turn off outputting formatting first if saving the entire page, or simply set-and-save the 'ajasta' field, eg (untested): wire()->addHookAfter('Pages::published', function($event) { $page = $event->arguments('page'); $t = wire()->templates->get($page->template); if($t->name == 'artikkeli' && $t->hasField("ajasta") { $page->setAndSave('ajasta', 0); } $event->return = $page; }); 4
Autofahrn Posted April 11, 2019 Posted April 11, 2019 I guess $pages is not "ready" in _init.php, since _init.php is loaded pretty early. When PW is up and running and before the template is executed, it runs site/ready.php. So placing hooks in ready.php is the recommended practice. 28 minutes ago, psy said: site/ready.php loads before _init.php sorry, no. 1
wbmnfktr Posted April 11, 2019 Posted April 11, 2019 I'd support the /site/ready.php solution as well. It seems to be the better place for hooks that don't end in a module. But there is another thing. Your hooks is tied to the published state or the action of publishing it. While testing this you have to unplublish the page first to test your hook. Had this testing issue a while back, too. ? 1
wbmnfktr Posted April 11, 2019 Posted April 11, 2019 Be careful here @Autofahrn and @psy. @VeiJari is talking about init.php and not _init.php. init.php ready.php finished.php Just to keep this in mind. https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/ 5
Autofahrn Posted April 11, 2019 Posted April 11, 2019 Sure, init.php runs before ready.php as well, but thanks for the link, which explains this more precisely: Quote /site/init.php This file is included during ProcessWire's boot initialization, immediately after autoload modules have been loaded and had their init() methods called. Anything you do in here will behave the same as an init() method on a module. When this file is called, the current $page has not yet been determined. This is an excellent place to attach hooks that don't need to know anything about the current page. 3
psy Posted April 12, 2019 Posted April 12, 2019 Good pick-up @wbmnfktr! /site/init.php is different to /site/templates/_init.php I guess the custom of using ready.php is that by that time, PW knows about the current page which in this case, is referenced in the hook. 2
VeiJari Posted April 16, 2019 Author Posted April 16, 2019 On 4/11/2019 at 3:27 PM, psy said: Hi @VeiJari It's usual practice, but not compulsory, to put hooks into /site/ready.php. site/ready.php loads before _init.php. You're changing the saved page data so you may need to turn off outputting formatting first if saving the entire page, or simply set-and-save the 'ajasta' field, eg (untested): wire()->addHookAfter('Pages::published', function($event) { $page = $event->arguments('page'); $t = wire()->templates->get($page->template); if($t->name == 'artikkeli' && $t->hasField("ajasta") { $page->setAndSave('ajasta', 0); } $event->return = $page; }); Tested and the code works! Thank you for this. Our customer also wants the field to be disabled when the page is published, how do you achieve this? Quick browsing of the API, there doesn't seem to be a disable() function for a field.
VeiJari Posted April 16, 2019 Author Posted April 16, 2019 On 4/11/2019 at 4:02 PM, wbmnfktr said: Be careful here @Autofahrn and @psy. @VeiJari is talking about init.php and not _init.php. init.php ready.php finished.php Just to keep this in mind. https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/ I was actually talking about /site/templates/_init.php, but after reading the difference of /site/init.php and /site/templates/_init.php, I decided to include psys' solution to site/init.php. Thank you for this! 1
wbmnfktr Posted April 16, 2019 Posted April 16, 2019 3 minutes ago, VeiJari said: Our customer also wants the field to be disabled when the page is published, how do you achieve this? Quick browsing of the API, there doesn't seem to be a disable() function for a field. I have never used it but you could try to hook into the fields visibility via API in some way. https://processwire.com/docs/fields/dependencies/
Autofahrn Posted April 16, 2019 Posted April 16, 2019 7 minutes ago, VeiJari said: Our customer also wants the field to be disabled when the page is published, how do you achieve this? Quick browsing of the API, there doesn't seem to be a disable() function for a field. You are probably looking for the "collapsed" field and its constants. (collapsedNoLocked to be precise) 1
VeiJari Posted April 16, 2019 Author Posted April 16, 2019 46 minutes ago, Autofahrn said: You are probably looking for the "collapsed" field and its constants. (collapsedNoLocked to be precise) I think the solution lies somewhere in that topic, but I haven't figured out how to integrate Robin S' solution for my Pages::published hook.
bernhard Posted April 16, 2019 Posted April 16, 2019 $wire->addHookAfter('ProcessPageEdit::buildForm', function($event) { $page = $event->object->getPage(); if($page->template != 'home') return; // example if($page->isUnpublished()) return; $form = $event->arguments(0); $field = $form->getChildByName('title'); // example $field->collapsed = Inputfield::collapsedNoLocked; }); ? 6
VeiJari Posted April 16, 2019 Author Posted April 16, 2019 58 minutes ago, bernhard said: $wire->addHookAfter('ProcessPageEdit::buildForm', function($event) { $page = $event->object->getPage(); if($page->template != 'home') return; // example if($page->isUnpublished()) return; $form = $event->arguments(0); $field = $form->getChildByName('title'); // example $field->collapsed = Inputfield::collapsedNoLocked; }); ? Works like a charm! Thank you for this! And also, I want to thank every one for motivating me to write hooks and seeing how practical and easy they're to make! ? 5
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