Skip to content
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

CollectionField - Add ability to specify a toString method #6202

Open
wants to merge 5 commits into
base: 4.x
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions doc/fields/CollectionField.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ used to render the form of each collection entry::

yield CollectionField::new('...')->setEntryType(SomeType::class);

setEntryToStringMethod
~~~~~~~~~~~~

By default, items in the collection are represented by a single line showing
their ``__toString()`` value. The ``setEntryToStringMethod()`` method defines the method
used to get string value of each item::

yield CollectionField::new('...')->setEntryToStringMethod('getFullName');

showEntryLabel
~~~~~~~~~~~~~~

Expand Down
9 changes: 9 additions & 0 deletions src/Field/CollectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class CollectionField implements FieldInterface
public const OPTION_ALLOW_DELETE = 'allowDelete';
public const OPTION_ENTRY_IS_COMPLEX = 'entryIsComplex';
public const OPTION_ENTRY_TYPE = 'entryType';
public const OPTION_ENTRY_TO_STRING_METHOD = 'entryToStringMethod';
public const OPTION_SHOW_ENTRY_LABEL = 'showEntryLabel';
public const OPTION_RENDER_EXPANDED = 'renderExpanded';
public const OPTION_ENTRY_USES_CRUD_FORM = 'entryUsesCrudController';
Expand All @@ -42,6 +43,7 @@ public static function new(string $propertyName, $label = null): self
->setCustomOption(self::OPTION_ALLOW_DELETE, true)
->setCustomOption(self::OPTION_ENTRY_IS_COMPLEX, null)
->setCustomOption(self::OPTION_ENTRY_TYPE, null)
->setCustomOption(self::OPTION_ENTRY_TO_STRING_METHOD, null)
->setCustomOption(self::OPTION_SHOW_ENTRY_LABEL, false)
->setCustomOption(self::OPTION_RENDER_EXPANDED, false)
->setCustomOption(self::OPTION_ENTRY_USES_CRUD_FORM, false)
Expand Down Expand Up @@ -82,6 +84,13 @@ public function setEntryType(string $formTypeFqcn): self
return $this;
}

public function setEntryToStringMethod(string $toStringMethod): self
{
$this->setCustomOption(self::OPTION_ENTRY_TO_STRING_METHOD, $toStringMethod);

return $this;
}

public function showEntryLabel(bool $showLabel = true): self
{
$this->setCustomOption(self::OPTION_SHOW_ENTRY_LABEL, $showLabel);
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/views/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
{% block collection_entry_row %}
{% set is_array_field = 'EasyCorp\\Bundle\\EasyAdminBundle\\Field\\ArrayField' == form_parent(form).vars.ea_vars.field.fieldFqcn ?? false %}
{% set is_complex = form_parent(form).vars.ea_vars.field.customOptions.get('entryIsComplex') ?? false %}
{% set to_string_method = form_parent(form).vars.ea_vars.field.customOptions.get('entryToStringMethod') %}
{% set allows_deleting_items = form_parent(form).vars.allow_delete|default(false) %}
{% set render_expanded = form_parent(form).vars.ea_vars.field.customOptions.get('renderExpanded') ?? false %}
{% set delete_item_button %}
Expand All @@ -198,7 +199,7 @@
<h2 class="accordion-header">
<button class="accordion-button {{ render_expanded ? '' : 'collapsed' }}" type="button" data-bs-toggle="collapse" data-bs-target="#{{ id }}-contents">
<i class="fas fw fa-chevron-right form-collection-item-collapse-marker"></i>
{{ value|ea_as_string }}
{{ value|ea_as_string(to_string_method) }}
</button>

{% if allows_deleting_items and not disabled %}
Expand Down
12 changes: 10 additions & 2 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function applyFilterIfExists(Environment $environment, $value, string $fi
throw new RuntimeError(sprintf('Invalid callback for filter: "%s"', $filterName));
}

public function representAsString($value): string
public function representAsString($value, ?string $toStringMethod = null): string
{
if (null === $value) {
return '';
Expand All @@ -159,12 +159,20 @@ public function representAsString($value): string
}

if (\is_object($value)) {
$callable = [$value, $toStringMethod];
if (\is_callable($callable)) {
$strVal = \call_user_func($callable);
if (\is_string($strVal)) {
return $strVal;
}
}

if ($value instanceof TranslatableInterface) {
return $value->trans($this->translator);
}

if (method_exists($value, '__toString')) {
return (string) $value;
$strVal = (string) $value;
}

if (method_exists($value, 'getId')) {
Expand Down
Loading