I would like to create my own format option for DetailView and GridView. I'm now using available formatter options like datetime:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'current_date:datetime'
]
]?>
I'd like to have my own formatter like this:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'current_date:datetime',
'colorId:color'
]
]?>
Where color would translate colorId (which is int - identifier of color) into color name. I know that I can have a function / virtual attribute in the model, but I would like to use it anywhere, not only on that certain model. I've been searching but found only that I need to have specific formatter.
You could extend the Formatter class and handle 'color' as a new type of format. Something like this...
Then switch to using this new formatter by changing your config...
Now you should be able to use the color format in a gridview/datacolumn like you asked:
...or in general by calling the app's formatter component:
Please note, this code is NOT tested and bugs might be included.