* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * */ namespace Sonata\AdminBundle\Form\Type; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\AbstractType; use Sonata\AdminBundle\Form\EventListener\MergeCollectionListener; use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList; use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer; use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer; use Sonata\AdminBundle\Model\ModelManagerInterface; class ModelType extends AbstractType { private $modelManager; public function __construct(ModelManagerInterface $modelManager) { $this->modelManager = $modelManager; } public function buildForm(FormBuilder $builder, array $options) { if ($options['multiple']) { $builder ->addEventSubscriber(new MergeCollectionListener($this->modelManager)) ->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list'])); } else { $builder->prependClientTransformer(new ModelToIdTransformer($this->modelManager, $options['class'])); } } public function getDefaultOptions(array $options) { $defaultOptions = array( 'template' => 'choice', 'multiple' => false, 'expanded' => false, 'model_manager' => $this->modelManager, 'class' => null, 'property' => null, 'query' => null, 'choices' => array(), 'preferred_choices' => array(), 'parent' => 'choice', 'field_description' => false, ); $options = array_replace($defaultOptions, $options); if (!isset($options['choice_list'])) { $defaultOptions['choice_list'] = new ModelChoiceList( $options['model_manager'], $options['class'], $options['property'], $options['query'], $options['choices'] ); } return $defaultOptions; } public function getParent(array $options) { return $options['parent']; } public function getName() { return 'sonata_model'; } }