Skip to content

Commit f798f73

Browse files
committed
[Autocomplete] Define a method to combine basic attributes with extra attributes rather than allowing to override the result entirely
1 parent b66fb61 commit f798f73

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +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
7+
- Add a `getAttributes()` method to define extra attributes to add to the autocomplete result.
88

99
## 2.22.0
1010

src/Autocomplete/src/AutocompleteResultsExecutor.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +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[] = self::getAutocompleterResult($autocompleter, $entity);
76+
$results[] = $this->formatResult($autocompleter, $entity);
7777
}
7878

7979
return new AutocompleteResults($results, $hasNextPage);
@@ -101,7 +101,7 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
101101
$optgroupLabels = [];
102102

103103
foreach ($paginator as $entity) {
104-
$result = self::getAutocompleterResult($autocompleter, $entity);
104+
$result = $this->formatResult($autocompleter, $entity);
105105

106106
$groupLabels = $groupBy($entity, $result['value'], $result['text']);
107107

@@ -122,13 +122,15 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
122122
/**
123123
* @return array<string, mixed>
124124
*/
125-
private static function getAutocompleterResult(EntityAutocompleterInterface $autocompleter, object $entity): array
125+
private function formatResult(EntityAutocompleterInterface $autocompleter, object $entity): array
126126
{
127-
if (method_exists($autocompleter, 'getResult')) {
128-
return $autocompleter->getResult($entity);
127+
$attributes = [];
128+
if (method_exists($autocompleter, 'getAttributes')) {
129+
$attributes = $autocompleter->getAttributes($entity);
129130
}
130131

131132
return [
133+
...$attributes,
132134
'value' => $autocompleter->getValue($entity),
133135
'text' => $autocompleter->getLabel($entity),
134136
];

src/Autocomplete/src/EntityAutocompleterInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
*
2121
* @template T of object
2222
*
23-
* @method array getResults(object $entity) Returns the autocomplete result, usually the label as "text" and the value as "value".
24-
* May be used to expose extra options such as "disabled" so that tom-select's rendering may be customized.
23+
* @method array getAttributes(object $entity) Returns extra attributes to add to the autocomplete result.
2524
* @method mixed getGroupBy() Return group_by option.
2625
*/
2726
interface EntityAutocompleterInterface
@@ -55,14 +54,13 @@ public function getLabel(object $entity): string;
5554
public function getValue(object $entity): mixed;
5655

5756
/**
58-
* Returns the autocomplete result, usually the label as "text" and the value as "value".
59-
* May be used to expose extra options such as "disabled" so that tom-select's rendering may be customized.
57+
* Returns extra attributes to add to the autocomplete result.
6058
*
6159
* @param T $entity
6260
*
6361
* @return array<string, mixed>
6462
*/
65-
/* public function getResult(object $entity): array; */
63+
/* public function getAttributes(object $entity): array; */
6664

6765
/**
6866
* Return true if access should be granted to the autocomplete results for the current user.

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

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

64-
public function getResult(object $entity): array
64+
public function getAttributes(object $entity): array
6565
{
66-
return [
67-
'text' => $this->getLabel($entity),
68-
'value' => $this->getValue($entity),
69-
];
66+
return [];
7067
}
7168

7269
public function isGranted(Security $security): bool

0 commit comments

Comments
 (0)