Skip to content

Commit f63ff7d

Browse files
jakubtobiaszkbond
authored andcommitted
[Autocomplete] Use choice_value in the WrappedEntityTypeAutocompleter in EntityAutocompleteField
1 parent a13900f commit f63ff7d

File tree

8 files changed

+144
-1
lines changed

8 files changed

+144
-1
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.17.0
4+
5+
- Allow `choice_value` option in entity autocomplete fields #1723
6+
37
## 2.16.0
48

59
- Missing translations added for many languages #1527 #1528 #1535

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public function getLabel(object $entity): string
100100

101101
public function getValue(object $entity): string
102102
{
103+
$choiceValue = $this->getFormOption('choice_value');
104+
105+
if (\is_string($choiceValue) || $choiceValue instanceof PropertyPathInterface) {
106+
return $this->propertyAccessor->getValue($entity, $choiceValue);
107+
}
108+
109+
if ($choiceValue instanceof \Closure) {
110+
return $choiceValue($entity);
111+
}
112+
103113
return $this->getEntityMetadata()->getIdValue($entity);
104114
}
105115

src/Autocomplete/tests/Fixtures/Entity/Category.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Category
2626
#[ORM\Column()]
2727
private ?string $name = null;
2828

29+
#[ORM\Column()]
30+
private ?string $code = null;
31+
2932
#[ORM\OneToMany(mappedBy: 'category', targetEntity: Product::class)]
3033
private Collection $products;
3134

@@ -51,6 +54,18 @@ public function setName(string $name): self
5154
return $this;
5255
}
5356

57+
public function setCode(string $code): self
58+
{
59+
$this->code = $code;
60+
61+
return $this;
62+
}
63+
64+
public function getCode(): ?string
65+
{
66+
return $this->code;
67+
}
68+
5469
/**
5570
* @return Collection<int, Product>
5671
*/

src/Autocomplete/tests/Fixtures/Factory/CategoryFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ final class CategoryFactory extends ModelFactory
3939
{
4040
protected function getDefaults(): array
4141
{
42+
$name = self::faker()->name();
4243
return [
43-
'name' => self::faker()->text(),
44+
'name' => $name,
45+
'code' => strtolower(str_replace(' ', '_', $name)),
4446
];
4547
}
4648

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Fixtures\Form;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
17+
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
18+
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;
19+
20+
#[AsEntityAutocompleteField]
21+
class CategoryWithCallbackAsCustomValue extends AbstractType
22+
{
23+
public function configureOptions(OptionsResolver $resolver): void
24+
{
25+
$resolver->setDefaults([
26+
'class' => Category::class,
27+
'placeholder' => 'What should we eat?',
28+
'choice_value' => function (?Category $category): ?string {
29+
return $category?->getCode();
30+
},
31+
]);
32+
}
33+
34+
public function getParent(): string
35+
{
36+
return BaseEntityAutocompleteType::class;
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Fixtures\Form;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
17+
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
18+
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;
19+
20+
#[AsEntityAutocompleteField]
21+
class CategoryWithPropertyNameAsCustomValue extends AbstractType
22+
{
23+
public function configureOptions(OptionsResolver $resolver): void
24+
{
25+
$resolver->setDefaults([
26+
'class' => Category::class,
27+
'placeholder' => 'What should we eat?',
28+
'choice_value' => 'code',
29+
]);
30+
}
31+
32+
public function getParent(): string
33+
{
34+
return BaseEntityAutocompleteType::class;
35+
}
36+
}

src/Autocomplete/tests/Fixtures/Kernel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
1515
use Doctrine\ORM\Mapping\AssociationMapping;
16+
use Fixtures\Form\CategoryWithCallbackAsCustomValue;
17+
use Fixtures\Form\CategoryWithPropertyNameAsCustomValue;
1618
use Psr\Log\NullLogger;
1719
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1820
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
@@ -179,6 +181,16 @@ protected function configureContainer(ContainerConfigurator $c): void
179181

180182
$services->alias('public.ux.autocomplete.make_autocomplete_field', 'ux.autocomplete.make_autocomplete_field')
181183
->public();
184+
185+
$services->set(CategoryWithPropertyNameAsCustomValue::class)
186+
->tag('ux.entity_autocomplete_field')
187+
->public()
188+
;
189+
190+
$services->set(CategoryWithCallbackAsCustomValue::class)
191+
->tag('ux.entity_autocomplete_field')
192+
->public()
193+
;
182194
}
183195

184196
protected function configureRoutes(RoutingConfigurator $routes): void

src/Autocomplete/tests/Functional/FieldAutocompleterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,30 @@ public function testItWorksWithoutAChoiceLabel(): void
101101
->assertJsonMatches('length(results)', 5)
102102
;
103103
}
104+
105+
public function testItUsesTheCustomStringValue(): void
106+
{
107+
$category = CategoryFactory::createOne(['code' => 'foo']);
108+
109+
$this->browser()
110+
->throwExceptions()
111+
->get('/test/autocomplete/category_with_property_name_as_custom_value?query=foo')
112+
->assertSuccessful()
113+
->assertJsonMatches('results[0].value', 'foo')
114+
->assertJsonMatches('results[0].text', $category->getName())
115+
;
116+
}
117+
118+
public function testItUsesTheCustomCallbackValue(): void
119+
{
120+
$category = CategoryFactory::createOne(['code' => 'foo']);
121+
122+
$this->browser()
123+
->throwExceptions()
124+
->get('/test/autocomplete/category_with_callback_as_custom_value?query=foo')
125+
->assertSuccessful()
126+
->assertJsonMatches('results[0].value', 'foo')
127+
->assertJsonMatches('results[0].text', $category->getName())
128+
;
129+
}
104130
}

0 commit comments

Comments
 (0)