Skip to content

Commit

Permalink
Merge pull request sonata-project#3655 from Mangetsu/sf3-model-loader…
Browse files Browse the repository at this point in the history
…-patch

Fix ChoiceLoader issue SF2.7+
  • Loading branch information
OskarStark committed Apr 2, 2016
2 parents 4642345 + 61a9118 commit c42f1d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 11 additions & 5 deletions Form/DataTransformer/ModelsToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sonata\AdminBundle\Form\DataTransformer;

use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\DataTransformerInterface;
Expand Down Expand Up @@ -44,14 +45,19 @@ class ModelsToArrayTransformer implements DataTransformerInterface
/**
* ModelsToArrayTransformer constructor.
*
* @param ModelChoiceList|LazyChoiceList $choiceList
* @param ModelManagerInterface $modelManager
* @param $class
* @param ModelChoiceList|LazyChoiceList|ModelChoiceLoader $choiceList
* @param ModelManagerInterface $modelManager
* @param $class
*
* @throws RuntimeException
*/
public function __construct($choiceList, ModelManagerInterface $modelManager, $class)
{
if (!$choiceList instanceof ModelChoiceList && !$choiceList instanceof LazyChoiceList) {
throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of ModelChoiceList or LazyChoiceList');
if (!$choiceList instanceof ModelChoiceList
&& !$choiceList instanceof ModelChoiceLoader
&& !$choiceList instanceof LazyChoiceList) {
throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of
ModelChoiceLoader or ModelChoiceList or LazyChoiceList');
}

$this->choiceList = $choiceList;
Expand Down
5 changes: 4 additions & 1 deletion Form/Type/ModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['multiple']) {
if (array_key_exists('choice_loader', $options) && $options['choice_loader'] !== null) { // SF2.7+
$builder->addViewTransformer(new ModelsToArrayTransformer($options['choice_list'], $options['model_manager'], $options['class']), true);
$builder->addViewTransformer(new ModelsToArrayTransformer(
$options['choice_loader'],
$options['model_manager'],
$options['class']), true);
} else {
$builder->addViewTransformer(new LegacyModelsToArrayTransformer($options['choice_list']), true);
}
Expand Down

0 comments on commit c42f1d6

Please sign in to comment.