Skip to content

Commit a5cea09

Browse files
committed
[Autocomplete][Turbo] Remove BC layers for methods and parameters
1 parent 871f288 commit a5cea09

11 files changed

+41
-54
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Remove `ParentEntityAutocompleteType` in favor of `BaseEntityAutocompleteType`
88
- Remove `ExtraLazyChoiceLoader` in favor of `Symfony\Component\Form\ChoiceList\Loader\LazyChoiceLoader` from Symfony Form >=7.2
99
- Add parameter `$security` to `AutocompleteResultsExecutor::__construct()`
10+
- Remove BC layer for `EntityAutocompleterInterface::getAttributes()` and `EntityAutocompleterInterface::getGroupBy()`
1011

1112
## 2.30
1213

@@ -52,7 +53,7 @@ class IngredientAutocompleteType extends AbstractType
5253

5354
- Deprecate `ExtraLazyChoiceLoader` in favor of `Symfony\Component\Form\ChoiceList\Loader\LazyChoiceLoader`
5455
- Reset TomSelect when updating url attribute #1505
55-
- Add `getAttributes()` method to define additional attributes for autocomplete results #2541
56+
- Add `EntityAutocompleterInterface::getAttributes()` method to define additional attributes for autocomplete results #2541
5657

5758
## 2.22.0
5859

src/Autocomplete/src/AutocompleteResultsExecutor.php

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

6060
$results = [];
6161

62-
if (!method_exists($autocompleter, 'getGroupBy') || null === $groupBy = $autocompleter->getGroupBy()) {
62+
if (null === $groupBy = $autocompleter->getGroupBy()) {
6363
foreach ($paginator as $entity) {
6464
$results[] = $this->formatResult($autocompleter, $entity);
6565
}
@@ -112,13 +112,8 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string
112112
*/
113113
private function formatResult(EntityAutocompleterInterface $autocompleter, object $entity): array
114114
{
115-
$attributes = [];
116-
if (method_exists($autocompleter, 'getAttributes')) {
117-
$attributes = $autocompleter->getAttributes($entity);
118-
}
119-
120115
return [
121-
...$attributes,
116+
...$autocompleter->getAttributes($entity),
122117
'value' => $autocompleter->getValue($entity),
123118
'text' => $autocompleter->getLabel($entity),
124119
];

src/Autocomplete/src/EntityAutocompleterInterface.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
* Interface for classes that will have an "autocomplete" endpoint exposed.
2020
*
2121
* @template T of object
22-
*
23-
* TODO Remove next lines for Symfony UX 3
24-
*
25-
* @method array getAttributes(object $entity) Returns extra attributes to add to the autocomplete result.
26-
* @method mixed getGroupBy() Return group_by option.
2722
*/
2823
interface EntityAutocompleterInterface
2924
{
@@ -58,9 +53,11 @@ public function getValue(object $entity): mixed;
5853
/**
5954
* Returns extra attributes to add to the autocomplete result.
6055
*
61-
* TODO Uncomment for Symfony UX 3
56+
* @param T $entity
57+
*
58+
* @return array<string, mixed>
6259
*/
63-
/* public function getAttributes(object $entity): array; */
60+
public function getAttributes(object $entity): array;
6461

6562
/**
6663
* Return true if access should be granted to the autocomplete results for the current user.
@@ -71,8 +68,6 @@ public function isGranted(Security $security): bool;
7168

7269
/*
7370
* Return group_by option.
74-
*
75-
* TODO Uncomment for Symfony UX 3
7671
*/
77-
/* public function getGroupBy(): mixed; */
72+
public function getGroupBy(): mixed;
7873
}

src/Autocomplete/src/Form/BaseEntityAutocompleteType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ public function configureOptions(OptionsResolver $resolver): void
6565
'security' => false,
6666
// set the max results number that a query on automatic endpoint return.
6767
'max_results' => 10,
68+
// extra attributes to add to the autocomplete result, either an array or a callable (called with the entity)
69+
'additional_attributes' => null,
6870
]);
6971

7072
$resolver->setAllowedTypes('security', ['boolean', 'string', 'callable']);
7173
$resolver->setAllowedTypes('max_results', ['int', 'null']);
7274
$resolver->setAllowedTypes('filter_query', ['callable', 'null']);
75+
$resolver->setAllowedTypes('additional_attributes', ['null', 'callable', 'array']);
7376
$resolver->setNormalizer('searchable_fields', function (Options $options, ?array $searchableFields) {
7477
if (null !== $searchableFields && null !== $options['filter_query']) {
7578
throw new RuntimeException('Both the searchable_fields and filter_query options cannot be set.');

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,28 @@ public function getGroupBy(): mixed
138138
return $this->getFormOption('group_by');
139139
}
140140

141+
public function getAttributes(object $entity): array
142+
{
143+
$attributesOption = $this->getFormOption('additional_attributes');
144+
145+
if (null === $attributesOption) {
146+
return [];
147+
}
148+
149+
if (\is_array($attributesOption)) {
150+
return $attributesOption;
151+
}
152+
153+
if (\is_callable($attributesOption)) {
154+
return $attributesOption($entity);
155+
}
156+
157+
return $attributesOption;
158+
}
159+
141160
private function getFormOption(string $name): mixed
142161
{
143162
$form = $this->getForm();
144-
// Remove when dropping support for ParentEntityAutocompleteType
145-
$form = $form->has('autocomplete') ? $form->get('autocomplete') : $form;
146163
$formOptions = $form->getConfig()->getOptions();
147164

148165
return $formOptions[$name] ?? null;

src/Turbo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Minimum required Symfony version is now 6.4
66
- Minimum required PHP version is now 8.2
77
- Remove old compatibility layer with deprecated `StimulusTwigExtension` from WebpackEncoreBundle ^1.0, use StimulusBundle instead
8+
- Remove BC layer for `TurboStreamListenRendererInterface::renderTurboStreamListen()` `$eventSourceOptions` parameter
89

910
## 2.30
1011

src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Mercure\Twig\MercureExtension;
1616
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1717
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
18-
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface;
18+
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface;
1919
use Twig\Environment;
2020
use Twig\Error\RuntimeError;
2121

@@ -24,7 +24,7 @@
2424
*
2525
* @author Kévin Dunglas <kevin@dunglas.fr>
2626
*/
27-
final class TurboStreamListenRenderer implements TurboStreamListenRendererWithOptionsInterface
27+
final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface
2828
{
2929
public function __construct(
3030
private HubInterface $hub,
@@ -34,12 +34,8 @@ public function __construct(
3434
) {
3535
}
3636

37-
public function renderTurboStreamListen(Environment $env, $topic /* array $eventSourceOptions = [] */): string
37+
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string
3838
{
39-
if (\func_num_args() > 2) {
40-
$eventSourceOptions = func_get_arg(2);
41-
}
42-
4339
$topics = $topic instanceof TopicSet
4440
? array_map($this->resolveTopic(...), $topic->getTopics())
4541
: [$this->resolveTopic($topic)];
@@ -51,7 +47,7 @@ public function renderTurboStreamListen(Environment $env, $topic /* array $event
5147
$controllerAttributes['topic'] = current($topics);
5248
}
5349

54-
if (isset($eventSourceOptions)) {
50+
if ([] !== $eventSourceOptions) {
5551
try {
5652
$mercure = $this->twig->getExtension(MercureExtension::class);
5753

src/Turbo/src/Twig/TurboRuntime.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public function renderTurboStreamListen(Environment $env, $topic, ?string $trans
5252

5353
$renderer = $this->turboStreamListenRenderers->get($transport);
5454

55-
return $renderer instanceof TurboStreamListenRendererWithOptionsInterface
56-
? $renderer->renderTurboStreamListen($env, $topic, $options) // @phpstan-ignore-line
57-
: $renderer->renderTurboStreamListen($env, $topic);
55+
return $renderer->renderTurboStreamListen($env, $topic, $options);
5856
}
5957
}

src/Turbo/src/Twig/TurboStreamListenRendererInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
interface TurboStreamListenRendererInterface
2222
{
2323
/**
24-
* @param string|object $topic
24+
* @param string|object $topic
25+
* @param array<string, mixed> $eventSourceOptions
2526
*/
26-
public function renderTurboStreamListen(Environment $env, $topic /* , array $eventSourceOptions = [] */): string;
27+
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string;
2728
}

src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)