diff --git a/src/DetailView.php b/src/DetailView.php index abf4d8fb4..53514e741 100644 --- a/src/DetailView.php +++ b/src/DetailView.php @@ -336,7 +336,10 @@ private function normalizeColumns(array $fields): array 'label' => Html::encode($field->label === '' ? $field->name : $field->label), 'labelAttributes' => $this->renderAttributes($labelAttributes), 'labelTag' => $labelTag, - 'value' => Html::encodeAttribute($this->renderValue($field->name, $field->value)), + 'value' => $field->encodeValue + ? Html::encodeAttribute($this->renderValue($field->name, $field->value)) + : (string) $this->renderValue($field->name, $field->value) + , 'valueAttributes' => $this->renderAttributes($valueAttributes), 'valueTag' => $valueTag, ]; diff --git a/src/Field/DataField.php b/src/Field/DataField.php index dd74bb697..86559a388 100644 --- a/src/Field/DataField.php +++ b/src/Field/DataField.php @@ -39,6 +39,8 @@ final class DataField * a function accepting data and returning the array. * Example array: `['class' => 'value', 'data-type' => 'text']` * Example closure: `fn($data) => ['class' => $data->status . '-value']` + * + * @param bool $encodeValue Whether the value is HTML encoded */ public function __construct( public readonly string $name = '', @@ -47,7 +49,8 @@ public function __construct( public readonly string $labelTag = '', public readonly mixed $value = null, public readonly string $valueTag = '', - public readonly array|Closure $valueAttributes = [] + public readonly array|Closure $valueAttributes = [], + public readonly bool $encodeValue = true, ) { } } diff --git a/tests/DetailView/BaseTest.php b/tests/DetailView/BaseTest.php index e000e6fcb..79b0bc6c8 100644 --- a/tests/DetailView/BaseTest.php +++ b/tests/DetailView/BaseTest.php @@ -19,6 +19,41 @@ final class BaseTest extends TestCase { use TestTrait; + public function testNonEncodedValue(): void + { + Assert::equalsWithoutLE( + << +
+
+
id
+
1
+
+
+
value
+
tests 1
+
+
+
total
+
10
+
+
+ + HTML, + DetailView::widget() + ->fields( + new DataField('id'), + new DataField( + name: 'value', + encodeValue: false + ), + new DataField('total'), + ) + ->data(['id' => 1, 'value' => '
tests 1
', 'total' => '10']) + ->render(), + ); + } + /** * @throws InvalidConfigException * @throws NotFoundException