Reputation: 405
I want to give if condition to control the action buttons(view, edit and delete) in the template attribute
and also add new buttons.
I have this code, but something is wrong:
[
'class' => 'yii\grid\ActionColumn',
'template'=>function ($session){
if($session->get('userType') != 'admin'){
'{view}{update}{delete}';
}else{
'template' => '{view}{update}{delete}{activate}{deactivate}',
'buttons' => [
'deactivate' => function ($url,\backend\models\Document $model) {
if($model->Status==1)
return Html::a('<span class="glyphicon glyphicon glyphicon-remove"></span>', $url, [
'title' => Yii::t('app', 'deactivate'),
]);
},
'activate' => function ($url, $model) {
if($model->Status==0)
return Html::a('<span class="glyphicon glyphicon glyphicon-ok"></span>', $url, [
'title' => Yii::t('app', 'activate'),
]);
},
],
}
},
],
But I got this error :
Object of class Closure could not be converted to string
I am using session object as a parameter in the function. how to fix that, or what is the perfect way to check with if condition?
Upvotes: 1
Views: 669
Reputation: 133360
Don't use an anonymous function
'template'=>function ( ... ) {}
but call an external function or directly use a proper assigment by code eg:
'template'=> (if($session->get('userType') != 'admin')) ? '{view}{update}{delete}' : '{view}{update}{delete}{activate}{deactivate}',
public function myTemplate($session){
....
return yourResult;
}
then
'template'=> myTemplate($session),
Upvotes: 1