Skip to content

Add ability to not encode a DataField value #258

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

Merged
merged 5 commits into from
Mar 9, 2025
Merged
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
5 changes: 4 additions & 1 deletion src/DetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
5 changes: 4 additions & 1 deletion src/Field/DataField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand All @@ -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,
) {
}
}
35 changes: 35 additions & 0 deletions tests/DetailView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@ final class BaseTest extends TestCase
{
use TestTrait;

public function testNonEncodedValue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<dl>
<div>
<dt>id</dt>
<dd>1</dd>
</div>
<div>
<dt>value</dt>
<dd><pre>tests 1</pre></dd>
</div>
<div>
<dt>total</dt>
<dd>10</dd>
</div>
</dl>
</div>
HTML,
DetailView::widget()
->fields(
new DataField('id'),
new DataField(
name: 'value',
encodeValue: false
),
new DataField('total'),
)
->data(['id' => 1, 'value' => '<pre>tests 1</pre>', 'total' => '10'])
->render(),
);
}

/**
* @throws InvalidConfigException
* @throws NotFoundException
Expand Down
Loading