Skip to content

Instantly share code, notes, and snippets.

@bendavies
Created September 20, 2023 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendavies/b00a494f6ebbefc47213149514e52a70 to your computer and use it in GitHub Desktop.
Save bendavies/b00a494f6ebbefc47213149514e52a70 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Form\Type;
...
#[AsEntityAutocompleteField]
class CarrierAutocompleteField extends AbstractType
{
public function __construct(
private readonly UserContext $userContext,
) {
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Carrier::class,
'placeholder' => 'Search for a Carrier',
'searchable_fields' => ['name'],
'choice_label' => static function (Carrier $carrier): string {
$mga = $carrier->getMga();
$companyNumber = $carrier->getCompanyNumber();
return Str\format(
'%s%s%s',
$carrier->getName() ?? '',
null !== $mga ? Str\format(' - %s', $mga) : '',
null !== $companyNumber ? Str\format(' (%s)', $companyNumber) : '',
);
},
'query_builder' => function (CarrierRepository $repository) {
return $repository->createQueryBuilder('c')
->where('c.division = :division')
->setParameter('division', $this->userContext->getDivision())
;
},
'tom_select_options' => [
'plugins' => ['dropdown_input'],
],
]);
}
public function getParent(): string
{
return ParentEntityAutocompleteType::class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment