I made custom currency formatter + converter based on values from database.
This is how I use it in DetailView
use yii\helpers\Html;
use app\commands\AppHelper;
use yii\widgets\DetailView;
use app\models\Part;
<?= DetailView::widget([
'model' => $model,
'attributes' => [
// ...
[
'attribute' => 'price',
'label' => (new Part())->getAttributeLabel('price_user'),
'format' => [
'currency',
AppHelper::getUserCurrencyCode(),
[
'convert' => true,
'currencyFrom' => $model->currency->code,
'currencyTo' => AppHelper::getUserCurrencyCode(),
],
],
],
// ...
],
]) ?>
In this widget I can accomplish behaviour like this: when there is numeric value, it gets formatted, if there is NULL
value, usual (not-set) is printed out...
Notice $model->currency->code
which is data from relation, in DetailView
easily accessible but I can not figure out how to get that data into formatter
in GridView
.
Problem is when I want to format data in GridView
.
I allow NULL
values on column that I need to use formatter on, so I already threw away idea of using
'value' => function ($data, $key, $index, $column) { return $data->value; }
because when NULL
value is present, yii sends data like this
<span class="not-set">(not set)</span>
and either I want to let it be or set my custom value (considering different value for other columns with NULL
value) and I also want to save trouble handling all those (not set)
values.
Another reason is, as I noticed, that if I use 'format' => ...
in attribute params, formatting happens before setting those (not set)
values.
So I was thinking about somehow passing that $model->currency->code
, which is data from relation, to that formatter.
Any ideas? Thanks.
Worst case scenario I will use formatter in value dumping values that contains
'<span'
orNULL
like this, but it is ugly and I dont like it...EDIT: I added custom static method to format unset data. I still dont like it, but hey, it works ... :D
and in Part.php (Part model) I added method