Created
September 20, 2023 20:15
-
-
Save bendavies/b00a494f6ebbefc47213149514e52a70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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