Jump to content

Recommended Posts

Posted

I have options defined that I want to sort before display in admin.
The options are displayed as checkboxes, or AsmSelect depending on how I feel :D

Alphanumeric eg:
1=remoteGarage
2=secureParking
3=study
4=dishwasher
5=builtInRobes
6=gym

Sorted for display:
5=buildInRobes
4=dishwasher
6=gym
1=remoteGarage
2=secureParking
3=study

Posted
1 hour ago, Mackski said:

5=buildInRobes
4=dishwasher
6=gym
1=remoteGarage
2=secureParking
3=study

Not sure I understand, but you can just enter the options into the field config exactly like that and then there is no need for a hook.

Posted

I agree that @Robin S solution is definitely the easiest if that works for you.

But, if you do need to dynamically order with a hook, then this should work for you. Don't forget to change 'options_field_name' to the appropriate value.

$this->addHookAfter('Field::getInputfield', null, function($event) {
    if($event->object->name == 'options_field_name') {
        $inputfield = $event->return;
        $options = $inputfield->options;
        foreach($options as $id => $title) {
            $inputfield->removeOption($id, $title);
        }
        asort($options);
        foreach($options as $id => $title) {
            $inputfield->addOption($id, $title);
        }
        $event->return = $inputfield;
    }
});

I am a little under the weather today and I feel like I am missing an easier way to remove all existing options before sorting and adding them back. I feel like there is probably a better way to do this in general, but it doesn't look like you can use PW's sort() here. Maybe I am just not seeing it right now :)

  • Like 1
Posted
3 hours ago, Robin S said:

Not sure I understand, but you can just enter the options into the field config exactly like that and then there is no need for a hook.

I'm not sure why I didn't try this, I just assumed PW would just order them by ID, however this is not the case.

This also makes sense when adding new options in order with new ID's. Problem solved.

 

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
  • Recently Browsing   0 members

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