Skip to content

Add DataField visibility setting #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/DetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
}

/**
* @psalm-return list<

Check failure on line 303 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidReturnType

src/DetailView.php:303:22: InvalidReturnType: The declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns is incorrect, got 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' which is different due to additional array shape fields (isVisible) (see https://psalm.dev/011)

Check failure on line 303 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidReturnType

src/DetailView.php:303:22: InvalidReturnType: The declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns is incorrect, got 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' which is different due to additional array shape fields (isVisible) (see https://psalm.dev/011)

Check failure on line 303 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidReturnType

src/DetailView.php:303:22: InvalidReturnType: The declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns is incorrect, got 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' which is different due to additional array shape fields (isVisible) (see https://psalm.dev/011)

Check failure on line 303 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

InvalidReturnType

src/DetailView.php:303:22: InvalidReturnType: The declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns is incorrect, got 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' which is different due to additional array shape fields (isVisible) (see https://psalm.dev/011)
* array{
* label: string,
* labelAttributes: array<array-key, mixed>,
Expand Down Expand Up @@ -338,10 +338,11 @@
,
'valueAttributes' => $this->renderAttributes($valueAttributes),
'valueTag' => $valueTag,
'isVisible' => $field->isVisible,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip field already here and don't add it to $normalized.

];
}

return $normalized;

Check failure on line 345 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidReturnStatement

src/DetailView.php:345:16: InvalidReturnStatement: The inferred type 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' does not match the declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns due to additional array shape fields (isVisible) (see https://psalm.dev/128)

Check failure on line 345 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidReturnStatement

src/DetailView.php:345:16: InvalidReturnStatement: The inferred type 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' does not match the declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns due to additional array shape fields (isVisible) (see https://psalm.dev/128)

Check failure on line 345 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidReturnStatement

src/DetailView.php:345:16: InvalidReturnStatement: The inferred type 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' does not match the declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns due to additional array shape fields (isVisible) (see https://psalm.dev/128)

Check failure on line 345 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

InvalidReturnStatement

src/DetailView.php:345:16: InvalidReturnStatement: The inferred type 'list<array{isVisible: bool, label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' does not match the declared return type 'list<array{label: string, labelAttributes: array<array-key, mixed>, labelTag: string, value: string, valueAttributes: array<array-key, mixed>, valueTag: string}>' for Yiisoft\Yii\DataView\DetailView::normalizeColumns due to additional array shape fields (isVisible) (see https://psalm.dev/128)
}

private function renderAttributes(array|Closure $attributes): array
Expand Down Expand Up @@ -371,23 +372,25 @@
$rows = [];

foreach ($fields as $field) {
$label = strtr($this->labelTemplate, [
'{label}' => $field['label'],
'{tag}' => $field['labelTag'],
'{attributes}' => Html::renderTagAttributes($field['labelAttributes']),
]);

$value = strtr($this->valueTemplate, [
'{value}' => $field['value'],
'{tag}' => $field['valueTag'],
'{attributes}' => Html::renderTagAttributes($field['valueAttributes']),
]);

$rows[] = strtr($this->fieldTemplate, [
'{attributes}' => Html::renderTagAttributes($this->fieldAttributes),
'{label}' => $label,
'{value}' => $value,
]);
if ($field['isVisible']) {

Check failure on line 375 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidArrayOffset

src/DetailView.php:375:17: InvalidArrayOffset: Cannot access value on variable $field using offset value of 'isVisible', expecting 'label', 'labelAttributes', 'labelTag', 'value', 'valueAttributes' or 'valueTag' (see https://psalm.dev/115)

Check failure on line 375 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/DetailView.php:375:17: InvalidArrayOffset: Cannot access value on variable $field using offset value of 'isVisible', expecting 'label', 'labelAttributes', 'labelTag', 'value', 'valueAttributes' or 'valueTag' (see https://psalm.dev/115)

Check failure on line 375 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArrayOffset

src/DetailView.php:375:17: InvalidArrayOffset: Cannot access value on variable $field using offset value of 'isVisible', expecting 'label', 'labelAttributes', 'labelTag', 'value', 'valueAttributes' or 'valueTag' (see https://psalm.dev/115)

Check failure on line 375 in src/DetailView.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.4-ubuntu-latest

InvalidArrayOffset

src/DetailView.php:375:17: InvalidArrayOffset: Cannot access value on variable $field using offset value of 'isVisible', expecting 'label', 'labelAttributes', 'labelTag', 'value', 'valueAttributes' or 'valueTag' (see https://psalm.dev/115)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd skip the iteration instead of wrapping code. Reads a bit better.

Suggested change
if ($field['isVisible']) {
if (!$field['isVisible']) {
continue;
}

$label = strtr($this->labelTemplate, [
'{label}' => $field['label'],
'{tag}' => $field['labelTag'],
'{attributes}' => Html::renderTagAttributes($field['labelAttributes']),
]);

$value = strtr($this->valueTemplate, [
'{value}' => $field['value'],
'{tag}' => $field['valueTag'],
'{attributes}' => Html::renderTagAttributes($field['valueAttributes']),
]);

$rows[] = strtr($this->fieldTemplate, [
'{attributes}' => Html::renderTagAttributes($this->fieldAttributes),
'{label}' => $label,
'{value}' => $value,
]);
}
}

return implode("\n", $rows);
Expand Down
3 changes: 3 additions & 0 deletions src/Field/DataField.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ final class DataField
*
* @param bool $encodeValue Whether the value is HTML encoded
*
* @param bool $isVisible Whether the field is visible
*
* @template TData as array|object
* @psalm-param string|Stringable|int|float|(Closure(TData): string)|null $value
*/
Expand All @@ -58,6 +60,7 @@ public function __construct(
public readonly string $valueTag = '',
public readonly array|Closure $valueAttributes = [],
public readonly bool $encodeValue = true,
public readonly bool $isVisible = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public readonly bool $isVisible = true,
public readonly bool $visible = true,

For consistency with parameter $visible in DataColumn constructor.

) {
}
}
32 changes: 32 additions & 0 deletions tests/Field/DataFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,36 @@ public function testValueTag(): void
->render(),
);
}

/**
* @throws InvalidConfigException
* @throws NotFoundException
* @throws NotInstantiableException
* @throws CircularReferenceException
*/
public function testNotVisibleField(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<dl>
<div>
<dt>id</dt>
<dd>1</dd>
</div>
<div>
<dt>username</dt>
<dd>admin</dd>
</div>
HTML,
DetailView::widget()
->fields(
new DataField('id'),
new DataField('username'),
new DataField('isAdmin', isVisible: false),
)
->data(['id' => 1, 'username' => 'admin', 'isAdmin' => true])
->render(),
);
}
}
Loading