diff --git a/src/DetailView.php b/src/DetailView.php index 044856e0b..d9871db1e 100644 --- a/src/DetailView.php +++ b/src/DetailView.php @@ -338,6 +338,7 @@ private function normalizeColumns(array $fields): array , 'valueAttributes' => $this->renderAttributes($valueAttributes), 'valueTag' => $valueTag, + 'isVisible' => $field->isVisible, ]; } @@ -371,23 +372,25 @@ private function renderFields(): string $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']) { + $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); diff --git a/src/Field/DataField.php b/src/Field/DataField.php index 2f1ce7e25..94767e62f 100644 --- a/src/Field/DataField.php +++ b/src/Field/DataField.php @@ -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 */ @@ -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, ) { } } diff --git a/tests/Field/DataFieldTest.php b/tests/Field/DataFieldTest.php index 3a4686592..90a3118a3 100644 --- a/tests/Field/DataFieldTest.php +++ b/tests/Field/DataFieldTest.php @@ -300,4 +300,36 @@ public function testValueTag(): void ->render(), ); } + + /** + * @throws InvalidConfigException + * @throws NotFoundException + * @throws NotInstantiableException + * @throws CircularReferenceException + */ + public function testNotVisibleField(): void + { + Assert::equalsWithoutLE( + << +
+
+
id
+
1
+
+
+
username
+
admin
+
+ HTML, + DetailView::widget() + ->fields( + new DataField('id'), + new DataField('username'), + new DataField('isAdmin', isVisible: false), + ) + ->data(['id' => 1, 'username' => 'admin', 'isAdmin' => true]) + ->render(), + ); + } }