From f11f7498d71c49a0d248173a4b01fcd58a57bbed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 27 Nov 2023 13:23:36 +0100 Subject: [PATCH] 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. --- .../Form/Type/PickUserDynamicType.php | 6 +++++- .../public/module/pick-entity/index.js | 19 +++++++++++++++---- .../Resources/views/Form/fields.html.twig | 3 ++- .../Form/Type/PickPersonDynamicType.php | 5 ++++- .../Form/Type/PickThirdpartyDynamicType.php | 5 ++++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php index a12042dab..5272b4aad 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php @@ -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() diff --git a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js index 99f83e1e3..6e939734e 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js @@ -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); + } } } }, diff --git a/src/Bundle/ChillMainBundle/Resources/views/Form/fields.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Form/fields.html.twig index 28c55c9c1..1ff92fd5f 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Form/fields.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Form/fields.html.twig @@ -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') }}"> + data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}" + data-as-id="{{ form.vars['as_id'] }}"> {% endblock %} {% block pick_postal_code_widget %} diff --git a/src/Bundle/ChillPersonBundle/Form/Type/PickPersonDynamicType.php b/src/Bundle/ChillPersonBundle/Form/Type/PickPersonDynamicType.php index 440b6568c..94c625a38 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/PickPersonDynamicType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/PickPersonDynamicType.php @@ -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() diff --git a/src/Bundle/ChillThirdPartyBundle/Form/Type/PickThirdpartyDynamicType.php b/src/Bundle/ChillThirdPartyBundle/Form/Type/PickThirdpartyDynamicType.php index 9022fb6dc..de233c360 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/Type/PickThirdpartyDynamicType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/Type/PickThirdpartyDynamicType.php @@ -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()