22
33namespace Tetranz \Select2EntityBundle \Form \Type ;
44
5+ use Tetranz \Select2EntityBundle \Form \DataTransformer \EntitiesToPropertyTransformer ;
6+ use Tetranz \Select2EntityBundle \Form \DataTransformer \EntityToPropertyTransformer ;
7+
58use Doctrine \Common \Persistence \ObjectManager ;
9+ use Doctrine \Common \Persistence \ManagerRegistry ;
610use Symfony \Component \Form \AbstractType ;
711use Symfony \Component \Form \DataTransformerInterface ;
812use Symfony \Component \Form \FormBuilderInterface ;
913use Symfony \Component \Form \FormInterface ;
1014use Symfony \Component \Form \FormView ;
1115use Symfony \Component \OptionsResolver \OptionsResolver ;
12- use Symfony \Component \OptionsResolver \OptionsResolverInterface ;
1316use Symfony \Component \Routing \RouterInterface ;
14- use Tetranz \Select2EntityBundle \Form \DataTransformer \EntitiesToPropertyTransformer ;
15- use Tetranz \Select2EntityBundle \Form \DataTransformer \EntityToPropertyTransformer ;
1617use Symfony \Component \PropertyAccess \PropertyAccess ;
1718
1819/**
2223 */
2324class Select2EntityType extends AbstractType
2425{
26+ /** @var ManagerRegistry */
27+ protected $ registry ;
2528 /** @var ObjectManager */
2629 protected $ em ;
2730 /** @var RouterInterface */
2831 protected $ router ;
29- /** @var array */
32+ /** @var array */
3033 protected $ config ;
3134
3235 /**
33- * @param ObjectManager $em
34- * @param RouterInterface $router
35- * @param array $config
36+ * @param ManagerRegistry $registry
37+ * @param RouterInterface $router
38+ * @param array $config
3639 */
37- public function __construct (ObjectManager $ em , RouterInterface $ router , $ config )
40+ public function __construct (ManagerRegistry $ registry , RouterInterface $ router , $ config )
3841 {
39- $ this ->em = $ em ;
42+ $ this ->registry = $ registry ;
43+ $ this ->em = $ registry ->getManager ();
4044 $ this ->router = $ router ;
4145 $ this ->config = $ config ;
4246 }
4347
4448 public function buildForm (FormBuilderInterface $ builder , array $ options )
4549 {
4650 // custom object manager for this entity, override the default entity manager ?
47- if (isset ($ options ['object_manager ' ])) {
51+ if (isset ($ options ['object_manager ' ])) {
4852 $ em = $ options ['object_manager ' ];
49- if (!$ em instanceof ObjectManager) {
53+ if (!$ em instanceof ObjectManager) {
5054 throw new \Exception ('The entity manager \'em \' must be an ObjectManager instance ' );
5155 }
5256 // Use the custom manager instead.
5357 $ this ->em = $ em ;
58+ } else if (isset ($ this ->config ['object_manager ' ])) {
59+ $ em = $ this ->registry ->getManager ($ this ->config ['object_manager ' ]);
60+ if (!$ em instanceof ObjectManager) {
61+ throw new \Exception ('The entity manager \'em \' must be an ObjectManager instance ' );
62+ }
63+ $ this ->em = $ em ;
64+ }
65+ else {
66+ $ manager = $ this ->registry ->getManagerForClass ($ options ['class ' ]);
67+ if ($ manager instanceof ObjectManager) {
68+ $ this ->em = $ manager ;
69+ }
5470 }
5571
5672 // add custom data transformer
@@ -70,9 +86,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7086
7187 // add the default data transformer
7288 } else {
73-
74- $ newTagPrefix = isset ($ options ['allow_add ' ]['new_tag_prefix ' ]) ? $ options ['allow_add ' ]['new_tag_prefix ' ] : $ this ->config ['allow_add ' ]['new_tag_prefix ' ];
75- $ newTagText = isset ($ options ['allow_add ' ]['new_tag_text ' ]) ? $ options ['allow_add ' ]['new_tag_text ' ] : $ this ->config ['allow_add ' ]['new_tag_text ' ];
89+ $ newTagPrefix = $ options ['allow_add ' ]['new_tag_prefix ' ] ?? $ this ->config ['allow_add ' ]['new_tag_prefix ' ];
90+ $ newTagText = $ options ['allow_add ' ]['new_tag_text ' ] ?? $ this ->config ['allow_add ' ]['new_tag_text ' ];
7691
7792 $ transformer = $ options ['multiple ' ]
7893 ? new EntitiesToPropertyTransformer ($ this ->em , $ options ['class ' ], $ options ['text_property ' ], $ options ['primary_key ' ], $ newTagPrefix , $ newTagText )
@@ -87,10 +102,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
87102 parent ::finishView ($ view , $ form , $ options );
88103 // make variables available to the view
89104 $ view ->vars ['remote_path ' ] = $ options ['remote_path ' ]
90- ?: $ this ->router ->generate ($ options ['remote_route ' ], array_merge ($ options ['remote_params ' ], [ 'page_limit ' => $ options ['page_limit ' ] ]));
105+ ?: $ this ->router ->generate ($ options ['remote_route ' ], array_merge ($ options ['remote_params ' ], ['page_limit ' => $ options ['page_limit ' ] ]));
91106
92107 // merge variable names which are only set per instance with those from yml config
93- $ varNames = array_merge (array ( 'multiple ' , 'placeholder ' , 'primary_key ' , 'autostart ' ) , array_keys ($ this ->config ));
108+ $ varNames = array_merge ([ 'multiple ' , 'placeholder ' , 'primary_key ' , 'autostart ' , ' query_parameters ' ] , array_keys ($ this ->config ));
94109 foreach ($ varNames as $ varName ) {
95110 $ view ->vars [$ varName ] = $ options [$ varName ];
96111 }
@@ -109,11 +124,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
109124 //tags options
110125 $ varNames = array_keys ($ this ->config ['allow_add ' ]);
111126 foreach ($ varNames as $ varName ) {
112- if (isset ($ options ['allow_add ' ][$ varName ])) {
113- $ view ->vars ['allow_add ' ][$ varName ] = $ options ['allow_add ' ][$ varName ];
114- } else {
115- $ view ->vars ['allow_add ' ][$ varName ] = $ this ->config ['allow_add ' ][$ varName ];
116- }
127+ $ view ->vars ['allow_add ' ][$ varName ] = $ options ['allow_add ' ][$ varName ] ?? $ this ->config ['allow_add ' ][$ varName ];
117128 }
118129
119130 if ($ options ['multiple ' ]) {
@@ -123,73 +134,52 @@ public function finishView(FormView $view, FormInterface $form, array $options)
123134 $ view ->vars ['class_type ' ] = $ options ['class_type ' ];
124135 }
125136
126- /**
127- * Added for pre Symfony 2.7 compatibility
128- *
129- * @param OptionsResolverInterface $resolver
130- */
131- public function setDefaultOptions (OptionsResolverInterface $ resolver )
132- {
133- $ this ->configureOptions ($ resolver );
134- }
135-
136137 /**
137138 * @param OptionsResolver $resolver
138139 */
139140 public function configureOptions (OptionsResolver $ resolver )
140141 {
141- $ resolver ->setDefaults (
142- array (
143- 'object_manager ' => null ,
142+ $ resolver ->setDefaults ([
143+ 'object_manager ' => null ,
144144 'class ' => null ,
145145 'data_class ' => null ,
146146 'primary_key ' => 'id ' ,
147147 'remote_path ' => null ,
148148 'remote_route ' => null ,
149- 'remote_params ' => array () ,
149+ 'remote_params ' => [] ,
150150 'multiple ' => false ,
151151 'compound ' => false ,
152152 'minimum_input_length ' => $ this ->config ['minimum_input_length ' ],
153153 'page_limit ' => $ this ->config ['page_limit ' ],
154154 'scroll ' => $ this ->config ['scroll ' ],
155155 'allow_clear ' => $ this ->config ['allow_clear ' ],
156- 'allow_add ' => array (
156+ 'allow_add ' => [
157157 'enabled ' => $ this ->config ['allow_add ' ]['enabled ' ],
158158 'new_tag_text ' => $ this ->config ['allow_add ' ]['new_tag_text ' ],
159159 'new_tag_prefix ' => $ this ->config ['allow_add ' ]['new_tag_prefix ' ],
160160 'tag_separators ' => $ this ->config ['allow_add ' ]['tag_separators ' ]
161- ) ,
161+ ] ,
162162 'delay ' => $ this ->config ['delay ' ],
163163 'text_property ' => null ,
164- 'placeholder ' => '' ,
164+ 'placeholder ' => false ,
165165 'language ' => $ this ->config ['language ' ],
166166 'required ' => false ,
167167 'cache ' => $ this ->config ['cache ' ],
168168 'cache_timeout ' => $ this ->config ['cache_timeout ' ],
169169 'transformer ' => null ,
170170 'autostart ' => true ,
171- 'width ' => isset ( $ this ->config ['width ' ]) ? $ this -> config [ ' width ' ] : null ,
172- 'req_params ' => array () ,
171+ 'width ' => $ this ->config ['width ' ] ?? null ,
172+ 'req_params ' => [] ,
173173 'property ' => null ,
174174 'callback ' => null ,
175175 'class_type ' => null ,
176- )
176+ 'query_parameters ' => [],
177+ 'render_html ' => $ this ->config ['render_html ' ] ?? false
178+ ]
177179 );
178180 }
179181
180182 /**
181- * pre Symfony 3 compatibility
182- *
183- * @return string
184- */
185- public function getName ()
186- {
187- return $ this ->getBlockPrefix ();
188- }
189-
190- /**
191- * Symfony 2.8+
192- *
193183 * @return string
194184 */
195185 public function getBlockPrefix ()
0 commit comments