Skip to content

Commit 05428f8

Browse files
committed
Improve the docs of autocomplete() customization
1 parent 49035dd commit 05428f8

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

doc/crud.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,13 @@ query, the optional :doc:`filters </filters>` and the pagination. If you need to
472472
fully customize this query, override the ``createIndexQueryBuilder()`` method in
473473
your CRUD controller.
474474

475+
.. _crud-autocomplete:
476+
475477
Autocomplete Options
476478
~~~~~~~~~~~~~~~~~~~~
477479

478-
Association fields allow you to :ref:`customize the autocomplete display <field-association-autocomplete>`.
480+
:doc:`Association fields </fields/AssociationField>` allow you to
481+
:ref:`customize the autocomplete display <field-association-autocomplete>` per field.
479482
You can also set a default autocomplete display for all association fields in a
480483
CRUD controller. This default display is applied to all fields that don't
481484
configure their own autocomplete, giving them a consistent formatting.
@@ -494,9 +497,7 @@ Define a callback that applies to all autocomplete fields::
494497
{
495498
return $crud
496499
->autocomplete(
497-
callback: static fn ($entity): string => method_exists($entity, 'getFullName')
498-
? $entity->getFullName()
499-
: (string) $entity
500+
callback: static fn ($entity): string => method_exists($entity, 'getFullName') ? $entity->getFullName() : (string) $entity
500501
)
501502
;
502503
}

doc/fields/AssociationField.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ By default, autocomplete fields display entities using their ``__toString()``
6161
method. You can customize this display using either a 1) callback (for simple
6262
text) or a 2) Twig template (for complex HTML).
6363

64+
.. tip::
65+
If you customize the autocomplete for a given class (for example, ``User``)
66+
in the same way across different CRUD controllers, you can
67+
:ref:`configure this globally <crud-autocomplete>` per CRUD and/or Dashboard.
68+
6469
**1) Simple Text Customization (Callback)**
6570

6671
Pass a callback to the ``autocomplete()`` method to customize how entities
@@ -69,11 +74,7 @@ appear in the dropdown. This is useful for adding extra information::
6974
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
7075

7176
yield AssociationField::new('city')->autocomplete(
72-
callback: static fn (City $c): string => sprintf(
73-
'%s, %s',
74-
$c->getName(),
75-
$c->getState()->getCode()
76-
)
77+
callback: static fn (City $c): string => sprintf('%s, %s', $c->getName(), $c->getState()->getCode())
7778
);
7879

7980
You can combine the ``enable`` parameter with other options::

0 commit comments

Comments
 (0)