mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 17:28:23 +00:00 
			
		
		
		
	Add new option "as_id" to Pick*DynamicType
This option will make the app return a single id of the entity in data, and not the entity json.
This commit is contained in:
		| @@ -42,6 +42,7 @@ class PickUserDynamicType extends AbstractType | ||||
|         $view->vars['types'] = ['user']; | ||||
|         $view->vars['uniqid'] = uniqid('pick_user_dyn'); | ||||
|         $view->vars['suggested'] = []; | ||||
|         $view->vars['as_id'] = true === $options['as_id'] ? '1' : '0'; | ||||
|  | ||||
|         foreach ($options['suggested'] as $user) { | ||||
|             $view->vars['suggested'][] = $this->normalizer->normalize($user, 'json', ['groups' => 'read']); | ||||
| @@ -54,7 +55,10 @@ class PickUserDynamicType extends AbstractType | ||||
|             ->setDefault('multiple', false) | ||||
|             ->setAllowedTypes('multiple', ['bool']) | ||||
|             ->setDefault('compound', false) | ||||
|             ->setDefault('suggested', []); | ||||
|             ->setDefault('suggested', []) | ||||
|             // if set to true, only the id will be set inside the content. The denormalization will not work. | ||||
|             ->setDefault('as_id', false) | ||||
|             ->setAllowedTypes('as_id', ['bool']); | ||||
|     } | ||||
|  | ||||
|     public function getBlockPrefix() | ||||
|   | ||||
| @@ -24,7 +24,8 @@ function loadDynamicPicker(element) { | ||||
|                     (input.value === '[]' || input.value === '') ? | ||||
|                         null : [ JSON.parse(input.value) ] | ||||
|                 ) | ||||
|             suggested = JSON.parse(el.dataset.suggested) | ||||
|             suggested = JSON.parse(el.dataset.suggested), | ||||
|             as_id = parseInt(el.dataset.asId) === 1; | ||||
|  | ||||
|         if (!isMultiple) { | ||||
|             if (input.value === '[]'){ | ||||
| @@ -50,7 +51,8 @@ function loadDynamicPicker(element) { | ||||
|                     types: JSON.parse(el.dataset.types), | ||||
|                     picked: picked === null ? [] : picked, | ||||
|                     uniqid: el.dataset.uniqid, | ||||
|                     suggested: suggested | ||||
|                     suggested: suggested, | ||||
|                     as_id: as_id, | ||||
|                 } | ||||
|             }, | ||||
|             computed: { | ||||
| @@ -69,7 +71,12 @@ function loadDynamicPicker(element) { | ||||
|                             return el.type === entity.type && el.id === entity.id; | ||||
|                         })) { | ||||
|                             this.picked.push(entity); | ||||
|                             input.value = JSON.stringify(this.picked); | ||||
|                             if (!as_id) { | ||||
|                               input.value = JSON.stringify(this.picked); | ||||
|                             } else { | ||||
|                               const ids = this.picked.map(el => el.id); | ||||
|                               input.value = ids.join(','); | ||||
|                             } | ||||
|                             console.log(entity) | ||||
|                         } | ||||
|                     } else { | ||||
| @@ -78,7 +85,11 @@ function loadDynamicPicker(element) { | ||||
|                         })) { | ||||
|                             this.picked.splice(0, this.picked.length); | ||||
|                             this.picked.push(entity); | ||||
|                             input.value = JSON.stringify(this.picked[0]); | ||||
|                             if (!as_id) { | ||||
|                               input.value = JSON.stringify(this.picked[0]); | ||||
|                             } else { | ||||
|                               input.value = this.picked.map(el => el.id); | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 }, | ||||
|   | ||||
| @@ -256,7 +256,8 @@ | ||||
|          data-types="{{ form.vars['types']|json_encode }}" | ||||
|          data-multiple="{{ form.vars['multiple'] }}" | ||||
|          data-uniqid="{{ form.vars['uniqid'] }}" | ||||
|          data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}"></div> | ||||
|          data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}" | ||||
|          data-as-id="{{ form.vars['as_id'] }}"></div> | ||||
| {% endblock %} | ||||
|  | ||||
| {% block pick_postal_code_widget %} | ||||
|   | ||||
| @@ -41,6 +41,7 @@ class PickPersonDynamicType extends AbstractType | ||||
|         $view->vars['types'] = ['person']; | ||||
|         $view->vars['uniqid'] = uniqid('pick_user_dyn'); | ||||
|         $view->vars['suggested'] = []; | ||||
|         $view->vars['as_id'] = true === $options['as_id'] ? '1' : '0'; | ||||
|  | ||||
|         foreach ($options['suggested'] as $person) { | ||||
|             $view->vars['suggested'][] = $this->normalizer->normalize($person, 'json', ['groups' => 'read']); | ||||
| @@ -53,7 +54,9 @@ class PickPersonDynamicType extends AbstractType | ||||
|             ->setDefault('multiple', false) | ||||
|             ->setAllowedTypes('multiple', ['bool']) | ||||
|             ->setDefault('compound', false) | ||||
|             ->setDefault('suggested', []); | ||||
|             ->setDefault('suggested', []) | ||||
|             ->setDefault('as_id', false) | ||||
|             ->setAllowedTypes('as_id', ['bool']); | ||||
|     } | ||||
|  | ||||
|     public function getBlockPrefix() | ||||
|   | ||||
| @@ -41,6 +41,7 @@ class PickThirdpartyDynamicType extends AbstractType | ||||
|         $view->vars['types'] = ['thirdparty']; | ||||
|         $view->vars['uniqid'] = uniqid('pick_user_dyn'); | ||||
|         $view->vars['suggested'] = []; | ||||
|         $view->vars['as_id'] = true === $options['as_id'] ? '1' : '0'; | ||||
|  | ||||
|         foreach ($options['suggested'] as $tp) { | ||||
|             $view->vars['suggested'][] = $this->normalizer->normalize($tp, 'json', ['groups' => 'read']); | ||||
| @@ -53,7 +54,9 @@ class PickThirdpartyDynamicType extends AbstractType | ||||
|             ->setDefault('multiple', false) | ||||
|             ->setAllowedTypes('multiple', ['bool']) | ||||
|             ->setDefault('compound', false) | ||||
|             ->setDefault('suggested', []); | ||||
|             ->setDefault('suggested', []) | ||||
|             ->setDefault('as_id', false) | ||||
|             ->setAllowedTypes('as_id', ['bool']); | ||||
|     } | ||||
|  | ||||
|     public function getBlockPrefix() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user