pjf5066 Posted October 14, 2014 Share Posted October 14, 2014 Happy Tuesday ! I am trying to sort an array based on a field. The field is a checkbox called user_terminated. I would like to sort this array on the date that the checkbox 'user_terminated' was checked. This will provide me a list of all terminated users from my system in order of the latest term on top. Thanks for any help! Parker Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 14, 2014 Share Posted October 14, 2014 ProcessWire doesn't store metadata of changes, it only stores the new values and that's it. If you want to get this functionality, you would need to build a small module which hooks into page::save and saves the date to a hidden date field, if the checkbox got checked. This way you can store and sort by date and status. 3 Link to comment Share on other sites More sharing options...
adrian Posted October 14, 2014 Share Posted October 14, 2014 PW doesn't store the date/time a field is changed. I think the easiest approach might be to add a hidden field called date_terminated. Then add a hook on page save that sets the date if the checkbox field has been checked. Place this is your admin.php file. wire()->addHookAfter('Pages::saveReady', function(HookEvent $event) { $page = $event->arguments("page"); if($page->user_terminated == 1 && $page->date_terminated == '') { $page->date_terminated = time(); } }); That is a little rough but will hopefully get you started. You should add an if statement to limit this to just the relevant template so it is not run on all pages. I am sure other improvements could also be made. PS Apparently I was writing this while LostKobrakai was responding 3 Link to comment Share on other sites More sharing options...
pjf5066 Posted October 14, 2014 Author Share Posted October 14, 2014 Thank you Link to comment Share on other sites More sharing options...
Soma Posted October 14, 2014 Share Posted October 14, 2014 Maybe a check to see if the checkbox has changed? $changed = $page->isChanged("mycheckbox"); Or there's a ___saved() hook ___saved(Page $page, array $changes = array(), $values = array()) Untested example wire()->addHookAfter('Pages::saved', function(HookEvent $event) { $page = $event->arguments("page"); if($page->template != "mytemplate") return; $changes = $event->arguments("changes"); // array of changed fields if(in_array("myfield", $changes){ // do stuff } }); 1 Link to comment Share on other sites More sharing options...
OLSA Posted June 2, 2017 Share Posted June 2, 2017 Hello, today I need this and try adrian and Soma solutions but with partial success (PW 3.0.42). Finally solved with this: wire()->addHookBefore('Pages::saveReady', function(HookEvent $event) { $page = $event->arguments('page'); if($page->template != "your_specific_template_name") return; if($page->isChanged('your_target_field_name')){ // your code $this->message('Your message about changes...'); } }); Main difference is "changes" argument, or $page->isChanged('some_field_name') Regards. 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