aleXela
aleXela

Reputation: 1301

OctoberCMS show value in list available instead of 1 and unavailable instead of 0

I have this in model:

 fields.yaml
    special_offer:
        label: special_offer
        type: Switch
        default: true

DB field is tinyint(1)

working good!

But I want to show in list not 0 or 1, but available/not available. I can store string, but I prefer to convert it.

this accessor:

public function getSpecialOfferAttribute ($value){
        return ( $value === 1 ) ? 'available' : 'not available' ;
}

will show right in list, but wrong in form, since switch accepts only 0/1

How can I do this?

Thanks

Upvotes: 0

Views: 499

Answers (1)

dragontree
dragontree

Reputation: 1799

You could use a custom column type for that. Define the custom column type in you plugin.php file:

public function registerListColumnTypes()
{
    return [
        // Convert special offer values to text
        'special_offer' => function($value) {
            $map = [
                0 => 'not available',       
                1 => 'available',       
            ];
            return $map[$value];
        }
    ];
}

And then in the list column definitions file use this:

special_offer:
    label: Special offer
    type: special_offer

Upvotes: 1

Related Questions