Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBridge] Extracted the common type and made the choice list g…
…eneric
- Loading branch information
Showing
3 changed files
with
113 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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 Symfony\Bridge\Doctrine\Form\Type; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Symfony\Component\Form\FormBuilder; | ||
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; | ||
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; | ||
use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener; | ||
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer; | ||
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
|
||
abstract class DoctrineType extends AbstractType | ||
{ | ||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
protected $registry; | ||
|
||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
if ($options['multiple']) { | ||
$builder | ||
->addEventSubscriber(new MergeCollectionListener()) | ||
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list'])) | ||
; | ||
} else { | ||
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list'])); | ||
} | ||
} | ||
|
||
public function getDefaultOptions(array $options) | ||
{ | ||
$defaultOptions = array( | ||
'em' => null, | ||
'class' => null, | ||
'property' => null, | ||
'query_builder' => null, | ||
'loader' => null, | ||
'choices' => null, | ||
'group_by' => null, | ||
); | ||
|
||
$options = array_replace($defaultOptions, $options); | ||
|
||
if (!isset($options['choice_list'])) { | ||
$manager = $this->registry->getManager($options['em']); | ||
if (isset($options['query_builder'])) { | ||
$options['loader'] = $this->getLoader($manager, $options); | ||
} | ||
|
||
$defaultOptions['choice_list'] = new EntityChoiceList( | ||
$manager, | ||
$options['class'], | ||
$options['property'], | ||
$options['loader'], | ||
$options['choices'], | ||
$options['group_by'] | ||
); | ||
} | ||
|
||
return $defaultOptions; | ||
} | ||
|
||
/** | ||
* Return the default loader object. | ||
* | ||
* @param ObjectManager $manager | ||
* @param array $options | ||
* @return EntityLoaderInterface | ||
*/ | ||
abstract protected function getLoader(ObjectManager $manager, array $options); | ||
|
||
public function getParent(array $options) | ||
{ | ||
return 'choice'; | ||
} | ||
} |
This file contains 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