Skip to content

Commit c3f06ba

Browse files
simondaigreweaverryan
authored andcommitted
[Autocomplete] Allow to edit route
1 parent 697e184 commit c3f06ba

File tree

7 files changed

+91
-4
lines changed

7 files changed

+91
-4
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Add `assets/src` to `.gitattributes` to exclude them from the installation
66
- Fix minCharacters option default value handling when using a falsy value like 0.
77
- Fix TypeScript types
8+
- Add a new `route` parameter to `AsEntityAutocompleteField`, which allows to choose another route for Ajax calls.
9+
- Fix minCharacters option default value handling when using a falsy value like 0.
10+
- Fix TypeScript types
811

912
## 2.6.0
1013

src/Autocomplete/doc/index.rst

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,34 @@ After creating this class, use it in your form:
159159
these won't be used during the Ajax call to fetch results. Instead, include
160160
all options inside the custom class (``FoodAutocompleteField``).
161161

162+
.. note::
163+
164+
.. versionadded:: Unreleased
165+
The default route for Autocomplete components is ``/autocomplete/{alias}/``
166+
Sometimes it may be useful to customize this URL - e.g. so that the component lives
167+
under a specific firewall.
168+
169+
To use another route, you need to declare it :
170+
171+
.. code-block:: yaml
172+
# config/routes/attributes.yaml
173+
ux_entity_autocomplete_admin:
174+
controller: ux.autocomplete.entity_autocomplete_controller
175+
path: '/admin/autocomplete/{alias}'
176+
177+
then specify this new route to your component :
178+
179+
.. code-block:: diff
180+
// src/Form/FoodAutocompleteField.php
181+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
182+
- #[AsEntityAutocompleteField]
183+
+ #[AsEntityAutocompleteField(route: 'ux_entity_autocomplete_admin')]
184+
class FoodAutocompleteField
185+
{
186+
// ...
187+
}
188+
189+
162190
Congratulations! Your ``EntityType`` is now Ajax-powered!
163191

164192
Styling Tom Select
@@ -493,10 +521,10 @@ consider registering the needed type extension ``AutocompleteChoiceTypeExtension
493521

494522
// tests/Form/Type/TestedTypeTest.php
495523
namespace App\Tests\Form\Type;
496-
524+
497525
use Symfony\Component\Form\Test\TypeTestCase;
498526
use Symfony\UX\Autocomplete\Form\AutocompleteChoiceTypeExtension;
499-
527+
500528
class TestedTypeTest extends TypeTestCase
501529
{
502530
protected function getTypeExtensions(): array
@@ -505,7 +533,7 @@ consider registering the needed type extension ``AutocompleteChoiceTypeExtension
505533
new AutocompleteChoiceTypeExtension(),
506534
];
507535
}
508-
536+
509537
// ... your tests
510538
}
511539

src/Autocomplete/src/Form/AsEntityAutocompleteField.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AsEntityAutocompleteField
2323
{
2424
public function __construct(
2525
private ?string $alias = null,
26+
private string $route = 'ux_entity_autocomplete',
2627
) {
2728
}
2829

@@ -31,6 +32,11 @@ public function getAlias(): ?string
3132
return $this->alias;
3233
}
3334

35+
public function getRoute(): string
36+
{
37+
return $this->route;
38+
}
39+
3440
public static function shortName(string $class): string
3541
{
3642
$string = new UnicodeString($class);

src/Autocomplete/src/Form/ParentEntityAutocompleteType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4141
}
4242

4343
// Use the provided URL, or auto-generate from the provided alias
44-
$autocompleteUrl = $options['autocomplete_url'] ?? $this->urlGenerator->generate('ux_entity_autocomplete', [
44+
$autocompleteUrl = $options['autocomplete_url'] ?? $this->urlGenerator->generate($attribute->getRoute(), [
4545
'alias' => $attribute->getAlias() ?: AsEntityAutocompleteField::shortName(\get_class($formType)),
4646
]);
4747

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\OptionsResolver\OptionsResolver;
7+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
8+
use Symfony\UX\Autocomplete\Form\ParentEntityAutocompleteType;
9+
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Ingredient;
10+
11+
#[AsEntityAutocompleteField(route: 'ux_autocomplete_alternate')]
12+
class AlternateRouteAutocompleteType extends AbstractType
13+
{
14+
public function configureOptions(OptionsResolver $resolver)
15+
{
16+
$resolver->setDefaults([
17+
'class' => Ingredient::class,
18+
'choice_label' => function(Ingredient $ingredient) {
19+
return $ingredient->getName();
20+
},
21+
'multiple' => true,
22+
]);
23+
}
24+
25+
public function getParent(): string
26+
{
27+
return ParentEntityAutocompleteType::class;
28+
}
29+
}

src/Autocomplete/tests/Fixtures/Kernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,8 @@ protected function configureRoutes(RoutingConfigurator $routes): void
158158
->prefix('/test/autocomplete');
159159

160160
$routes->add('test_form', '/test-form')->controller('kernel::testForm');
161+
162+
$routes->add('ux_autocomplete_alternate', '/alt/test/autocomplete/{alias}')
163+
->controller('ux.autocomplete.entity_autocomplete_controller');
161164
}
162165
}

src/Autocomplete/tests/Functional/CustomAutocompleterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,22 @@ public function testItReturns404OnBadAlias(): void
104104
->assertStatus(404)
105105
;
106106
}
107+
108+
public function testItWorksWithCustomRoute(): void
109+
{
110+
$product = ProductFactory::createOne(['name' => 'foo']);
111+
ProductFactory::createOne(['name' => 'bar']);
112+
ProductFactory::createOne(['name' => 'foo and bar']);
113+
114+
$this->browser()
115+
->throwExceptions()
116+
->get('/alt/test/autocomplete/custom_product')
117+
->assertSuccessful()
118+
->assertJsonMatches('length(results)', 3)
119+
->assertJsonMatches('results[0].value', $product->getId())
120+
->assertJsonMatches('results[0].text', 'foo')
121+
->get('/test/autocomplete/custom_product?query=bar')
122+
->assertJsonMatches('length(results)', 2)
123+
;
124+
}
107125
}

0 commit comments

Comments
 (0)