Skip to content

Commit 220bd68

Browse files
committed
[Autocomplete] Let the EntityAutocompleter drive results creation, so that extra options may be exposed for tom-select to use them
1 parent 6232fb5 commit 220bd68

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Deprecate `ExtraLazyChoiceLoader` in favor of `Symfony\Component\Form\ChoiceList\Loader\LazyChoiceLoader`
66
- Reset TomSelect when updating url attribute #1505
7+
- Let the EntityAutocompleter drive results creation, so that extra options may be exposed for tom-select to use them
78

89
## 2.22.0
910

src/Autocomplete/src/AutocompleteResultsExecutor.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
7373

7474
if (!method_exists($autocompleter, 'getGroupBy') || null === $groupBy = $autocompleter->getGroupBy()) {
7575
foreach ($paginator as $entity) {
76-
$results[] = [
77-
'value' => $autocompleter->getValue($entity),
78-
'text' => $autocompleter->getLabel($entity),
79-
];
76+
$results[] = $autocompleter->getResult($entity);
8077
}
8178

8279
return new AutocompleteResults($results, $hasNextPage);
@@ -104,10 +101,7 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
104101
$optgroupLabels = [];
105102

106103
foreach ($paginator as $entity) {
107-
$result = [
108-
'value' => $autocompleter->getValue($entity),
109-
'text' => $autocompleter->getLabel($entity),
110-
];
104+
$result = $autocompleter->getResult($entity);
111105

112106
$groupLabels = $groupBy($entity, $result['value'], $result['text']);
113107

src/Autocomplete/src/EntityAutocompleterInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function getLabel(object $entity): string;
5252
*/
5353
public function getValue(object $entity): mixed;
5454

55+
/**
56+
* Returns the autocomplete result, usually the label as "text" and the value as "value".
57+
* May be used to expose extra options such as "disabled" so that tom-select's rendering may be customized.
58+
*
59+
* @param T $entity
60+
*/
61+
public function getResult(object $entity): array;
62+
5563
/**
5664
* Return true if access should be granted to the autocomplete results for the current user.
5765
*

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public function getValue(object $entity): string
114114
return $this->getEntityMetadata()->getIdValue($entity);
115115
}
116116

117+
public function getResult(object $entity): array
118+
{
119+
return [
120+
'value' => $this->getValue($entity),
121+
'text' => $this->getLabel($entity),
122+
];
123+
}
124+
117125
public function isGranted(Security $security): bool
118126
{
119127
$securityOption = $this->getForm()->getConfig()->getOption('security');

src/Autocomplete/tests/Fixtures/Autocompleter/CustomProductAutocompleter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public function getValue(object $entity): mixed
6161
return $entity->getId();
6262
}
6363

64+
public function getResult(object $entity): array
65+
{
66+
return [
67+
'text' => $this->getLabel($entity),
68+
'value' => $this->getValue($entity),
69+
];
70+
}
71+
6472
public function isGranted(Security $security): bool
6573
{
6674
if ($this->requestStack->getCurrentRequest()?->query->get('enforce_test_security')) {

0 commit comments

Comments
 (0)