Skip to content

[Autocomplete] Use choice_value in the WrappedEntityTypeAutocompleter in EntityAutocompleteField #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Autocomplete/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.17.0

- Allow `choice_value` option in entity autocomplete fields #1723

## 2.16.0

- Missing translations added for many languages #1527 #1528 #1535
Expand Down
10 changes: 10 additions & 0 deletions src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public function getLabel(object $entity): string

public function getValue(object $entity): string
{
$choiceValue = $this->getFormOption('choice_value');

if (\is_string($choiceValue) || $choiceValue instanceof PropertyPathInterface) {
return $this->propertyAccessor->getValue($entity, $choiceValue);
}

if ($choiceValue instanceof \Closure) {
return $choiceValue($entity);
}

return $this->getEntityMetadata()->getIdValue($entity);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Autocomplete/tests/Fixtures/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Category
#[ORM\Column()]
private ?string $name = null;

#[ORM\Column()]
private ?string $code = null;

#[ORM\OneToMany(mappedBy: 'category', targetEntity: Product::class)]
private Collection $products;

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

public function setCode(string $code): self
{
$this->code = $code;

return $this;
}

public function getCode(): ?string
{
return $this->code;
}

/**
* @return Collection<int, Product>
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Autocomplete/tests/Fixtures/Factory/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ final class CategoryFactory extends ModelFactory
{
protected function getDefaults(): array
{
$name = self::faker()->name();
return [
'name' => self::faker()->text(),
'name' => $name,
'code' => strtolower(str_replace(' ', '_', $name)),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;

#[AsEntityAutocompleteField]
class CategoryWithCallbackAsCustomValue extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Category::class,
'placeholder' => 'What should we eat?',
'choice_value' => function (?Category $category): ?string {
return $category?->getCode();
},
]);
}

public function getParent(): string
{
return BaseEntityAutocompleteType::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fixtures\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;

#[AsEntityAutocompleteField]
class CategoryWithPropertyNameAsCustomValue extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Category::class,
'placeholder' => 'What should we eat?',
'choice_value' => 'code',
]);
}

public function getParent(): string
{
return BaseEntityAutocompleteType::class;
}
}
12 changes: 12 additions & 0 deletions src/Autocomplete/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Mapping\AssociationMapping;
use Fixtures\Form\CategoryWithCallbackAsCustomValue;
use Fixtures\Form\CategoryWithPropertyNameAsCustomValue;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand Down Expand Up @@ -179,6 +181,16 @@ protected function configureContainer(ContainerConfigurator $c): void

$services->alias('public.ux.autocomplete.make_autocomplete_field', 'ux.autocomplete.make_autocomplete_field')
->public();

$services->set(CategoryWithPropertyNameAsCustomValue::class)
->tag('ux.entity_autocomplete_field')
->public()
;

$services->set(CategoryWithCallbackAsCustomValue::class)
->tag('ux.entity_autocomplete_field')
->public()
;
}

protected function configureRoutes(RoutingConfigurator $routes): void
Expand Down
26 changes: 26 additions & 0 deletions src/Autocomplete/tests/Functional/FieldAutocompleterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,30 @@ public function testItWorksWithoutAChoiceLabel(): void
->assertJsonMatches('length(results)', 5)
;
}

public function testItUsesTheCustomStringValue(): void
{
$category = CategoryFactory::createOne(['code' => 'foo']);

$this->browser()
->throwExceptions()
->get('/test/autocomplete/category_with_property_name_as_custom_value?query=foo')
->assertSuccessful()
->assertJsonMatches('results[0].value', 'foo')
->assertJsonMatches('results[0].text', $category->getName())
;
}

public function testItUsesTheCustomCallbackValue(): void
{
$category = CategoryFactory::createOne(['code' => 'foo']);

$this->browser()
->throwExceptions()
->get('/test/autocomplete/category_with_callback_as_custom_value?query=foo')
->assertSuccessful()
->assertJsonMatches('results[0].value', 'foo')
->assertJsonMatches('results[0].text', $category->getName())
;
}
}