Skip to content
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
14 changes: 14 additions & 0 deletions doc/fields/TextField.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ pages (``new`` and ``edit``) contents are never truncated in length.
This option is ignored when also using the ``renderAsHtml()`` option, to
avoid truncating contents in the middle of an opened HTML tag.

``renderAsLinkToDetailAction``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, text contents are displayed as plain text. If you want the field
value to be rendered as a link to the entity's detail page on the ``index``
page, use this option::

yield TextField::new('...')->renderAsLinkToDetailAction();

This is useful when you want to let users click on a text value (e.g. a name
or a title) to navigate directly to the entity's detail page.

This option only has effect on the ``index`` page.

``stripTags``
~~~~~~~~~~~~~

Expand Down
9 changes: 9 additions & 0 deletions src/Field/TextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class TextField implements FieldInterface

public const OPTION_MAX_LENGTH = 'maxLength';
public const OPTION_RENDER_AS_HTML = 'renderAsHtml';
public const OPTION_RENDER_AS_LINK_TO_DETAIL_ACTION = 'renderAsLinkToDetailAction';
public const OPTION_STRIP_TAGS = 'stripTags';

public static function new(string $propertyName, TranslatableInterface|string|bool|null $label = null): self
Expand All @@ -28,6 +29,7 @@ public static function new(string $propertyName, TranslatableInterface|string|bo
->setDefaultColumns('col-md-6 col-xxl-5')
->setCustomOption(self::OPTION_MAX_LENGTH, null)
->setCustomOption(self::OPTION_RENDER_AS_HTML, false)
->setCustomOption(self::OPTION_RENDER_AS_LINK_TO_DETAIL_ACTION, false)
->setCustomOption(self::OPTION_STRIP_TAGS, false);
}

Expand All @@ -53,6 +55,13 @@ public function renderAsHtml(bool $asHtml = true): self
return $this;
}

public function renderAsLinkToDetailAction(bool $asLink = true): self
{
$this->setCustomOption(self::OPTION_RENDER_AS_LINK_TO_DETAIL_ACTION, $asLink);

return $this;
}

public function stripTags(bool $stripTags = true): self
{
$this->setCustomOption(self::OPTION_STRIP_TAGS, $stripTags);
Expand Down
8 changes: 7 additions & 1 deletion templates/crud/field/text.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
{% if ea().crud.currentAction == 'detail' %}
{% if field.customOptions.get('renderAsLinkToDetailAction') and ea().crud.currentAction == 'index' %}
{% set url = ea_url()
.setAction('detail')
.setEntityId(entity.primaryKeyValue)
%}
<a href="{{ url }}" title="{{ field.value }}">{{ field.formattedValue|raw }}</a>
{% elseif ea().crud.currentAction == 'detail' %}
<span title="{{ field.value }}">{{ field.formattedValue|raw|nl2br }}</span>
{% else %}
<span title="{{ field.value }}">{{ field.formattedValue|raw }}</span>
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Field/TextFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ public function testHtmlEntitiesAreEscapedByDefault(): void
self::assertStringContainsString('&lt;script&gt;', $fieldDto->getFormattedValue());
}

public function testRenderAsLinkToDetailAction(): void
{
$field = TextField::new('foo');
$field->renderAsLinkToDetailAction();
$fieldDto = $this->configure($field);

self::assertTrue($fieldDto->getCustomOption(TextField::OPTION_RENDER_AS_LINK_TO_DETAIL_ACTION));
}

public function testRenderAsLinkToDetailActionDefaultIsFalse(): void
{
$field = TextField::new('foo');
$fieldDto = $this->configure($field);

self::assertFalse($fieldDto->getCustomOption(TextField::OPTION_RENDER_AS_LINK_TO_DETAIL_ACTION));
}

public function testBackedEnumValue(): void
{
// create a backed enum for testing
Expand Down
Loading