Jump to content

Sort array


pjf5066
 Share

Recommended Posts

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

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.

  • Like 3
Link to comment
Share on other sites

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 :)

  • Like 3
Link to comment
Share on other sites

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
    }
    
});
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...