Skip to content

Commit 70f9959

Browse files
committed
feature #625 [Autocomplete] Allow to edit route (simondaigre, weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- [Autocomplete] Allow to edit route | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Fix #... | License | MIT Same issue as #562 but for Autocomplete component. `@weaverryan` `@kbond` let me know if this implementation is OK for you, then I will add tests, docs & changelog. Commits ------- 70e2276 [Autocomplete] Tweaking new route customization docs c3f06ba [Autocomplete] Allow to edit route
2 parents 697e184 + 70e2276 commit 70f9959

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
@@ -277,6 +277,34 @@ all of the options - separated by the ``delimiter`` - will be sent as a string.
277277
You *can* add autocompletion to this via the ``autocomplete_url`` option - but you'll
278278
likely need to create your own :ref:`custom autocomplete endpoint <custom-autocomplete-endpoint>`.
279279

280+
Customizing the AJAX URL/Route
281+
------------------------------
282+
283+
.. versionadded:: 2.7
284+
285+
The ability to specify the route was added in Twig Components 2.7.
286+
287+
The default route for the Ajax calls used by the Autocomplete component is ``/autocomplete/{alias}/``.
288+
Sometimes it may be useful to customize this URL - e.g. so that the URL lives
289+
under a specific firewall.
290+
291+
To use another route, first declare it:
292+
293+
.. code-block:: yaml
294+
295+
# config/routes/attributes.yaml
296+
ux_entity_autocomplete_admin:
297+
controller: ux.autocomplete.entity_autocomplete_controller
298+
path: '/admin/autocomplete/{alias}'
299+
300+
Then specify this new route on the attribute::
301+
302+
// src/Form/FoodAutocompleteField.php
303+
304+
#[AsEntityAutocompleteField(route: 'ux_entity_autocomplete_admin')]
305+
class FoodAutocompleteField
306+
// ...
307+
280308
Extending Tom Select
281309
--------------------
282310

@@ -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)