The following code should be added to the bootstrap of your yii application. If you would like to keep it in it's own file (recommended) create a config/giiant.php
, then include it from your config/bootstrap.php
.
Use a DateTimePicker for the scheduled_at
field:
\Yii::$container->set('schmunk42\giiant\crud\providers\DateTimeProvider', [
'columnNames' => ['scheduled_at'],
]);
The callback provider allows you to override the values of model attributes during code generation. There are 3 sections this applies to:
columnFormats
- used inGridView
on theindex
view.attributeFormats
- used inDetailView
on theview
view.activeFields
,prependActiveFields
andappendActiveFields
- used inActiveForm
on the_form
view.
Below is an example of the model we are working with. The methods required are:
optsStatus
- return dropdown data in theActiveForm
on theform
view.getStatusLabel
- return html string to be used inGridView
andDetailView
on theindex
andview
pages.
<?php
namespace app\models;
use Yii;
class Job extends base\Job
{
const STATUS_REQUESTED = 'requested';
const STATUS_SCHEDULED = 'scheduled';
const STATUS_DELIVERED = 'delivered';
public static function optsStatus($type = null)
{
return [
null => '',
self::STATUS_REQUESTED => self::STATUS_REQUESTED,
self::STATUS_SCHEDULED => self::STATUS_SCHEDULED,
self::STATUS_DELIVERED => self::STATUS_DELIVERED,
];
}
public function getStatusLabel()
{
return '<span class="label label-status-' . $this->status . '">' . $this->status . '</span>';
}
}
Display a column using a callback to Job::getStatusLabel()
:
$columnJobStatus = function ($attribute, $generator) {
return <<<INPUT
[
'attribute' => '{$attribute->name}',
'format' => 'raw',
'value' => function (\$model) {
return \$model->getStatusLabel();
},
]
INPUT;
};
$columnMultiLine = function ($attribute, $generator) {
return <<<INPUT
[
'attribute' => '{$attribute->name}',
'format' => 'ntext',
]
INPUT;
};
Join them into $columnFormats
, using the key as a regex to the model and attribute:
$columnFormats = [
'Job.status' => $columnJobStatus,
'.*\.address$|.*\.notes$' => $columnMultiLine,
];
Display an attribute using a callback to Job::getStatusLabel()
:
$attributeJobStatus = function ($attribute, $generator) {
return <<<INPUT
[
'format' => 'html',
'attribute' => '{$attribute->name}',
'value' => \$model->getStatusLabel(),
]
INPUT;
};
Display multi-line attributes with ntext
format:
$attributeMultiLine = function ($attribute, $generator) {
return <<<INPUT
[
'format' => 'ntext',
'attribute' => '{$attribute->name}',
]
INPUT;
};
Join them into $attributeFormats
, using the key as a regex to the model and attribute:
$attributeFormats = [
'Job.status' => $attributeJobStatus,
'.*\.address$|.*\.notes$' => $attributeMultiLine,
];
Use a radio list field:
$fieldJobType = function ($attribute, $generator) {
$data = \yii\helpers\VarDumper::export(['pickup' => Yii::t('app', 'Pickup'), 'delivery' => Yii::t('app', 'Delivery')]);
return <<<INPUT
\$form->field(\$model, '{$attribute->name}')->radioList({$data});
INPUT;
},
Customise the select field (the generator will already create this, only needed if you want to tweak it):
$fieldJobStatus = function ($attribute, $generator) {
return <<<INPUT
$form->field($model, '{$attribute->name}')->dropDownList(Job::optsStatus());
INPUT;
},
Use a text area for multi-line attributes:
$fieldMultiLine = function ($attribute, $generator) {
return <<<INPUT
\$form->field(\$model, '{$attribute->name}')->textarea();
INPUT;
},
Join them into $activeFields
, using the key as a regex to the model and attribute:
$activeFields = [
'Job.type' => $fieldJobType,
'Job.status' => $fieldJobStatus,
'.*\.address$|.*\.notes$' => $fieldMultiLine,
];
If we want to output something before or after the ActiveField
, we can use prependActiveFields
and appendActiveFields
.
For example we only want to show the fields that are safe:
$prependFieldSafe = function ($attribute, $generator) {
return <<<INPUT
if(\$model->isAttributeSafe('{$attribute->name}')) {
INPUT;
};
$appendFieldSafe = function ($attribute, $generator) {
return <<<INPUT
}
INPUT;
};
Join them into $prependActiveFields
and $appendActiveFields
, using the key as a regex to the model and attribute:
$prependActiveFields = [
'.*' => $prependFieldSafe,
];
$appendActiveFields = [
'.*' => $appendFieldSafe,
];
To invoke the callback provider we pass in our callbacks as follows:
\Yii::$container->set('schmunk42\giiant\crud\providers\CallbackProvider', [
'columnFormats' => $columnFormats,
'attributeFormats' => $attributeFormats,
'activeFields' => $activeFields,
'prependActiveFields' => $prependActiveFields,
'appendActiveFields' => $appendActiveFields,
]);
added to giiant docs: https://github.com/schmunk42/yii2-giiant/blob/master/docs/using-providers.md