1

Using Yii2, I'm trying to create a detailView. I want to hide empty rows, and therefore I use the kartik-v detailview. However, I also want to hide attributes if they conform to a certain condition. So I stumbled across this SO question, which captures the intention of my question. It does not, however, answer it satisfactory. (This question asks roughly the same thing). An example

<?= DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => [
        'id',
        'name', //cant be null, always shown
        'description:ntext', //can be null, so hidden thanks to kartiks detailview
        isAdmin() ? "password" :"", //an example, of course
        "hypotheticalOtherField",
        isAdmin() ? [
            'attribute'=>'client',
            'format'=>'raw',
            'value'=>function($object) {
                return Html::button("MyButton".$object->client);
            }
        ] : ""
    ]
])  ?>

As you can see, I want to show some fields based on (in this example) whether or not the user is admin. Sadly, inserting emtpy strings, empty arrays, or null values into the attributes array if the condition isn't met, produces an error (IE The attribute must be specified in the format of "attribute", "attribute:format" or "attribute:format:label" when inserting empty strings)

I suppose I could create the attributes array like this:

$attrs = ['id','name','description:ntext'];
if (isAdmin()) array_push($attrs, "password");
array_push($attrs, "hypotheticalOtherField");
if (isAdmin()) array_push($attrs, [
    'attribute'=>'client',
    'format'=>'raw',
    'value'=>function($object) {
        return Html::button("MyButton".$object->client);
    }
]);

echo DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => $attrs
]);

but then the overview with the standard Yii2 code layout is severely undermined.

So is there some way to conditionally insert values into an array, so I can keep coding Yii-style: estetic, organized, and uncluttered? Or maybe a values from which Yii2 knows it should be skipped when creating the View

3
  • Why do you think the attributes array is not good,? Your seems only a code styling problem....i post an aswer with you code coherent with yii2 style and good practice. see if you like Commented Dec 23, 2015 at 14:13
  • 'I want to hide empty rows, and therefore I use the kartik-v detailview' - you can do it with the default view, no need for third party addons Commented Dec 23, 2015 at 21:36
  • @Coz So how would one do that? I can't use the 'hideIfEmpty' attribute anymore, because that's kartiks attribute, yet I use kartiks detailview because of that attribute Commented Dec 28, 2015 at 8:41

2 Answers 2

4

You can use visible to DetailView

<?= DetailView::widget([
'hideIfEmpty' => true, //available in kartik's detailview
'model' => $model,
'attributes' => [
    'id',
    'name', //cant be null, always shown
    'description:ntext', //can be null, so hidden thanks to kartiks detailview
       [
            'visible' => (isAdmin() ? true : false),
            'value' => $model->password,
            'label' => 'test'
        ],    
])  ?>

Add whatever condition you want to add!!! in visible

Sign up to request clarification or add additional context in comments.

1 Comment

This indeed works, but not in Kartik's detailview. It only positions the 'visible'=>false attribute on the bottom of the detailview.
1

If you try with an array append shorthand and the look of the code is more yii2 stylish

The attribute based on array is a correct (good) practice.

$attrs[] = ['id','name','description:ntext'];

if (isAdmin()) {
    $attrs[] = ['password'];
}
$attrs[] = ['hypotheticalOtherField']

if (isAdmin()) {
    $attrs[] = [
        'attribute'=>'client',
        'format'=>'raw',
        'value'=>function($object) {
            return Html::button("MyButton".$object->client);
    }
}

echo DetailView::widget([
    'hideIfEmpty' => true, //available in kartik's detailview
    'model' => $model,
    'attributes' => $attrs
]);

1 Comment

I also do it just like this but I understand stealthjong's need for a shorter syntax. (without using kartik)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.