2929 */
3030
3131use Glpi \Application \View \TemplateRenderer ;
32+ use Glpi \DBAL \JsonFieldInterface ;
33+ use Glpi \Form \Condition \ConditionHandler \ItemAsTextConditionHandler ;
34+ use Glpi \Form \Condition \ConditionHandler \ItemConditionHandler ;
3235use Glpi \Form \Form ;
3336use Glpi \Form \Migration \FormQuestionDataConverterInterface ;
3437use Glpi \Form \Question ;
@@ -167,12 +170,24 @@ public function renderEndUserTemplate(Question $question): string
167170 $ default_value = json_decode ($ question ->fields ['default_value ' ], true );
168171 }
169172
173+ $ itemtype = null ;
174+ if (str_starts_with ((string ) $ current_field ->fields ['type ' ], 'dropdown ' )) {
175+ if ($ current_field ->fields ['type ' ] == 'dropdown ' ) {
176+ $ itemtype = PluginFieldsDropdown::getClassname ($ current_field ->fields ['name ' ]);
177+ } else {
178+ $ dropdown_matches = [];
179+ preg_match ('/^dropdown-(?<class>.+)$/ ' , (string ) $ current_field ->fields ['type ' ], $ dropdown_matches );
180+ $ itemtype = $ dropdown_matches ['class ' ];
181+ }
182+ }
183+
170184 $ twig = TemplateRenderer::getInstance ();
171185 return $ twig ->render ('@fields/question_type_end_user.html.twig ' , [
172186 'question ' => $ question ,
173187 'field ' => $ current_field ->fields ,
174188 'default_value ' => $ default_value ,
175189 'item ' => new Form (),
190+ 'itemtype ' => $ itemtype ,
176191 ]);
177192 }
178193
@@ -196,7 +211,8 @@ public function formatRawAnswer(mixed $answer, Question $question): string
196211 case 'date ' :
197212 return (string ) $ answer ;
198213 case 'dropdown ' :
199- if (is_string ($ answer )) {
214+ $ answer = $ answer ['items_id ' ];
215+ if (is_string ($ answer ) || is_numeric ($ answer )) {
200216 $ answer = [$ answer ];
201217 }
202218
@@ -283,6 +299,42 @@ public function getTargetQuestionType(array $rawData): string
283299 return self ::class;
284300 }
285301
302+ #[Override]
303+ public function getConditionHandlers (
304+ ?JsonFieldInterface $ question_config ,
305+ ): array {
306+ $ condition_handlers = parent ::getConditionHandlers ($ question_config );
307+ if (!$ question_config instanceof PluginFieldsQuestionTypeExtraDataConfig) {
308+ throw new InvalidArgumentException ();
309+ }
310+
311+ if (!$ question_config ->getFieldId ()) {
312+ return parent ::getConditionHandlers ($ question_config );
313+ }
314+
315+ // If the question is configured with a dropdown field, we add condition handlers to handle item and item as text conditions on the dropdown options
316+ $ field = PluginFieldsField::getById ($ question_config ->getFieldId ());
317+ if ($ field && str_starts_with ((string ) $ field ->fields ['type ' ], 'dropdown ' )) {
318+ if ($ field ->fields ['type ' ] == 'dropdown ' ) {
319+ $ itemtype = PluginFieldsDropdown::getClassname ($ field ->fields ['name ' ]);
320+ } else {
321+ $ dropdown_matches = [];
322+ preg_match ('/^dropdown-(?<class>.+)$/ ' , (string ) $ field ->fields ['type ' ], $ dropdown_matches );
323+ $ itemtype = $ dropdown_matches ['class ' ];
324+ }
325+
326+ $ condition_handlers = array_merge (
327+ $ condition_handlers ,
328+ [
329+ new ItemConditionHandler ($ itemtype ),
330+ new ItemAsTextConditionHandler ($ itemtype ),
331+ ],
332+ );
333+ }
334+
335+ return $ condition_handlers ;
336+ }
337+
286338 /**
287339 * Retrieve the default value block from the question's extra data
288340 *
0 commit comments