Skip to content

Commit

Permalink
Add HTML column type
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Jan 12, 2025
1 parent 6a5ad64 commit dabdbd6
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/src/reference/types/column/html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<script setup>
import ColumnTypeOptions from "./options/column.md";
</script>

# HtmlColumnType

The [`HtmlColumnType`](https://github.com/Kreyu/data-table-bundle/blob/main/src/Column/Type/HtmlColumnType.php) represents a column with value displayed as HTML.

## Options

### `raw`

- **type**: `bool`
- **default**: `true`

Defines whether the value should be rendered as raw HTML.

For example, if your column contains a string `<strong>Foo</strong>`:
- setting it to `true` will render the value as a bold text: **Foo**
- setting it to `false` will render the value as a plain text: `<strong>Foo</strong>`

### `strip_tags`

- **type**: `bool`
- **default**: `false`

Defines whether the tags should be stripped. Internally uses the [`strip_tags`](https://twig.symfony.com/doc/3.x/filters/striptags.html) function.

For example, if your column contains a string `<strong>Foo</strong>`:
- setting it to `true` will render the value as a simple text: Foo
- setting it to `false` will render the value as is: `<strong>Foo</strong>`

### `allowed_tags`

- **type**: `null`, `string` or `string[]`
- **default**: `null`

Defines tags which should not be stripped if `strip_tags` is set to `true`, e.g. `<br><p>`.

For example, if your column contains a string `<strong>Foo</strong><br/>`:
- setting it to `"<strong>"` will render the value as: `Foo<br/>`
- setting it to `"<strong><br>"` will render the value as: `<strong>Foo</strong><br/>`
- setting it to `["<strong>", "<br>"]` will render the value as: `<strong>Foo</strong><br/>`
- setting it to `null` (by default) will render the value as: `<strong>Foo</strong><br/>`

> [!WARNING]
> In Twig, this option is ignored if `strip_tags` option is set to `false`.
## Inherited options

<ColumnTypeOptions/>
44 changes: 44 additions & 0 deletions src/Column/Type/HtmlColumnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class HtmlColumnType extends AbstractColumnType
{
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
{
$view->vars = array_replace($view->vars, [
'raw' => $options['raw'],
'strip_tags' => $options['strip_tags'],
'allowed_tags' => $options['allowed_tags'],
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->define('raw')
->default(true)
->allowedTypes('bool')
->info('Defines whether the value should be rendered as raw HTML.')
;

/* @see https://www.php.net/strip_tags */
$resolver->define('strip_tags')
->default(false)
->allowedTypes('bool')
->info('Defines whether the tags should be stripped. Internally uses the "strip_tags" function.')
;

/* @see https://www.php.net/strip_tags */
$resolver->define('allowed_tags')
->default(null)
->allowedTypes('null', 'string', 'string[]')
->info('Defines tags which should not be stripped if "strip_tags" is set to true, e.g. "<br><p>"')
;
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Kreyu\Bundle\DataTableBundle\Column\Type\DateTimeColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\EnumColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\FormColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\HtmlColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\LinkColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\MoneyColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\NumberColumnType;
Expand Down Expand Up @@ -109,6 +110,11 @@
->tag('kreyu_data_table.column.type')
;

$services
->set('kreyu_data_table.column.type.html', HtmlColumnType::class)
->tag('kreyu_data_table.column.type')
;

$services
->set('kreyu_data_table.column.type.date_time', DateTimeColumnType::class)
->tag('kreyu_data_table.column.type')
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/views/themes/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@
</a>
{%- endblock %}

{% block column_html_value -%}
{% if strip_tags %}
{% set value = value|striptags(allowed_tags) %}
{% endif %}

{{ raw ? value|raw : value }}
{%- endblock %}

{% block column_date_time_value -%}
{% with { value: value ? value|date(format, timezone) : value } %}
{{- block('column_text_value') -}}
Expand Down
88 changes: 88 additions & 0 deletions tests/Unit/Column/Type/HtmlColumnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\HtmlColumnType;
use Kreyu\Bundle\DataTableBundle\Test\Column\Type\ColumnTypeTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class HtmlColumnTypeTest extends ColumnTypeTestCase
{
protected function getTestedColumnType(): ColumnTypeInterface
{
return new HtmlColumnType();
}

public function testDefaultRawOption(): void
{
$column = $this->createColumn();
$columnValueView = $this->createColumnValueView($column);

$this->assertTrue($columnValueView->vars['raw']);
}

#[DataProvider('provideRawOption')]
public function testPassingRawOption(bool $raw): void
{
$column = $this->createColumn(['raw' => $raw]);
$columnValueView = $this->createColumnValueView($column);

$this->assertEquals($raw, $columnValueView->vars['raw']);
}

public static function provideRawOption(): iterable
{
yield 'true' => [true];
yield 'false' => [false];
}

public function testDefaultStripTagsOption(): void
{
$column = $this->createColumn();
$columnValueView = $this->createColumnValueView($column);

$this->assertFalse($columnValueView->vars['strip_tags']);
}

#[DataProvider('provideStripTagsOption')]
public function testPassingStripTagsOption(bool $stripTags): void
{
$column = $this->createColumn(['strip_tags' => $stripTags]);
$columnValueView = $this->createColumnValueView($column);

$this->assertEquals($stripTags, $columnValueView->vars['strip_tags']);
}

public static function provideStripTagsOption(): iterable
{
yield 'true' => [true];
yield 'false' => [false];
}

public function testDefaultAllowedTagsOption(): void
{
$column = $this->createColumn();
$columnValueView = $this->createColumnValueView($column);

$this->assertNull($columnValueView->vars['allowed_tags']);
}

#[DataProvider('provideAllowedTagsOption')]
public function testPassingAllowedTagsOption(null|string|array $allowedTags): void
{
$column = $this->createColumn(['allowed_tags' => $allowedTags]);
$columnValueView = $this->createColumnValueView($column);

$this->assertEquals($allowedTags, $columnValueView->vars['allowed_tags']);
}

public static function provideAllowedTagsOption(): iterable
{
yield 'null' => [null];
yield 'string' => ['<strong><br>'];
yield 'array of strings' => [['<strong>', '<br>']];
}
}

0 comments on commit dabdbd6

Please sign in to comment.