{"id":6648,"date":"2025-01-29T21:59:18","date_gmt":"2025-01-29T19:59:18","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=6648"},"modified":"2025-01-29T21:59:19","modified_gmt":"2025-01-29T19:59:19","slug":"prestashop-trier-les-champs-du-formulaire-de-creation-de-compte","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2025\/01\/29\/prestashop-trier-les-champs-du-formulaire-de-creation-de-compte\/","title":{"rendered":"Prestashop : Trier les champs du formulaire de cr\u00e9ation de compte"},"content":{"rendered":"\n<p>A la suite de la cr\u00e9ation d&rsquo;un pr\u00e9c\u00e9dent module, pour lequel j&rsquo;ajoutais un champ dans le formulaire dans le formulaire de cr\u00e9ation de compte, j&rsquo;ai pas mal \u00e9t\u00e9 frustr\u00e9 de ne pas pouvoir le positionner ou je voulais dans ce formulaire.<br \/>Vous pouvez trouver le module ici : <a title=\"Prestashop : Ajouter un champ confirmation email sur le formulaire de cr\u00e9ation de compte\" href=\"https:\/\/www.h-hennes.fr\/blog\/2025\/01\/24\/prestashop-ajouter-un-champ-confirmation-email-sur-le-formulaire-de-creation-de-compte\/\">Prestashop : Ajouter un champ confirmation email sur le formulaire de cr\u00e9ation de compte<\/a><br \/>La solution que j&rsquo;avais trouv\u00e9 \u00e9tait de le faire en Javascript, mais ce n&rsquo;est pas la meilleure approche, et elle peut ne pas fonctionner sur tous les sites.<br \/><br \/>Le besoin \u00e9tant plus g\u00e9n\u00e9rique, j&rsquo;ai donc sorti un module sp\u00e9cifique pour g\u00e9rer ce comportement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fonctionnement technique<\/h2>\n\n\n\n<p>Pour ce besoin malheureusement pas d&rsquo;autres possibilit\u00e9s que de faire un override.<br \/>La logique de gestion est g\u00e9r\u00e9e dans la fonction getFormat de la classe CustomerFormatter ( dans classes\/form\/CustomerFormatter.php ).<br \/>Cette fonction propose bien un hook additionalCustomerFormFields pour ajouter des champs sp\u00e9cifiques appel\u00e9 ici : <\/p>\n\n\n\n<pre lang=\"php\">\n\/\/ ToDo, replace the hook exec with HookFinder when the associated PR will be merged\n$additionalCustomerFormFields = Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true);\n\nif (is_array($additionalCustomerFormFields)) {\n\tforeach ($additionalCustomerFormFields as $moduleName => $additionnalFormFields) {\n\t\tif (!is_array($additionnalFormFields)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tforeach ($additionnalFormFields as $formField) {\n\t\t\t$formField->moduleName = $moduleName;\n\t\t\t$format[$moduleName . '_' . $formField->getName()] = $formField;\n\t\t}\n\t}\n}\n<\/pre>\n\n\n\n<p>Mais le probl\u00e8me de ce hook et qu&rsquo;on peut uniquement modifier les champs natifs.<br \/>Si un autre module ajoute des champs sur le formulaire on ne peut les traiter, car ils n&rsquo;existent pas encore, et nous n&rsquo;avons pas acc\u00e8s aux champs des autres modules.<br \/>La surcharge \u00e0 faire est relativement facile , j&rsquo;ai mis des commentaires explicatifs.<\/p>\n\n\n\n<pre lang=\"php\">\n<?php\nclass CustomerFormatter extends CustomerFormatterCore\n{\n    public function getFormat()\n    {\n        \/\/On r\u00e9cup\u00e8re les donn\u00e9es de la m\u00e9thode parente avec TOUS les champs.\n        $format = parent::getFormat();\n        \/\/Pour plus de flexibilit\u00e9 on cr\u00e9\u00e9 un hook custom, et on passe la liste des champs dans la valeur format par r\u00e9f\u00e9rence\n        \/\/Ceci permets de le modifier directement la variable dans le hook.\n        Hook::exec('actionCustomerFormatterGetFormatAfter', ['format' => &$format]);\n        return $format;\n    }\n}\n<\/pre>\n\n\n\n<p>L&rsquo;objectif ensuite dans ce hook va \u00eatre de trier les champs selon l&rsquo;ordre qu&rsquo;on a choisi.<br \/>Pour cela dans le back office j&rsquo;ai fait une petite interface graphique qui r\u00e9cup\u00e8re la liste des champs.<br \/>Et qu&rsquo;on peut trier automatiquement.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2025\/01\/hhregistrationformsorter.png\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"529\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2025\/01\/hhregistrationformsorter.png\" alt=\"\" class=\"wp-image-6649\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2025\/01\/hhregistrationformsorter.png 671w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2025\/01\/hhregistrationformsorter-300x237.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/a><\/figure>\n\n\n\n<p>L&rsquo;ordre d\u00e9fini en back office est ensuite stock\u00e9 dans une configuration, avec le nom des champs sous la forme : <br \/><em>[\u00ab\u00a0id_gender\u00a0\u00bb,\u00a0\u00bbfirstname\u00a0\u00bb,\u00a0\u00bblastname\u00a0\u00bb,\u00a0\u00bbcompany\u00a0\u00bb,\u00a0\u00bbsiret\u00a0\u00bb,\u00a0\u00bbemail\u00a0\u00bb,\u00a0\u00bbhhregistrationconfirmemail_email_confirmation\u00a0\u00bb,\u00a0\u00bbpassword\u00a0\u00bb,\u00a0\u00bbbirthday\u00a0\u00bb,\u00a0\u00bboptin\u00a0\u00bb,\u00a0\u00bbps_dataprivacy_customer_privacy\u00a0\u00bb]<\/em><br \/>Et le tableau des champs est ensuite compar\u00e9 et\u00a0 tri\u00e9 selon l&rsquo;ordre des champs de cette configuration.<\/p>\n\n\n\n<pre lang=\"php\">\n     \/**\n     * Trie les champs du formulaire selon l'ordre d\u00e9fini\n     *\n     * Le tableau $fields est pass\u00e9 par r\u00e9f\u00e9rence pour \u00eatre modifi\u00e9 directement\n     *\n     * @param array $fields\n     * @param array $definedOrder\n     * @return void\n     *\/\n    private function sortFormFields(array &$fields, array $definedOrder)\n    {\n        $orderMap = array_flip($definedOrder);\n        uksort($fields, function ($a, $b) use ($orderMap) {\n            $posA = isset($orderMap[$a]) ? $orderMap[$a] : PHP_INT_MAX;\n            $posB = isset($orderMap[$b]) ? $orderMap[$b] : PHP_INT_MAX;\n            return $posA <=> $posB;\n        });\n    } \n<\/pre>\n\n\n\n<p>Plus besoin de d\u00e9pendre de tricks javascript on a donc maintenant une gestion du tri bien plus flexible !<br \/>Et la logique est relativement simple \u00e0 mettre en oeuvre.<br \/>Je me pose la question est-ce que ce hook aurait un int\u00e9r\u00eat \u00e0 \u00eatre natif ou est-ce que c&rsquo;est un besoin de niche ?<br \/>Vous pouvez t\u00e9l\u00e9charger le module directement sur la boutique<\/p>\n\n\n\n<p class=\"has-text-align-center has-luminous-vivid-amber-background-color has-background\"><a href=\"https:\/\/shop.h-hennes.fr\/fr\/20-tri-des-champs-du-formulaire-de-creation-de-compte.html\" target=\"_blank\" rel=\"noopener\" title=\"\">T\u00e9l\u00e9charger le module complet ( et gratuit ) sur la boutique<\/a><\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>A la suite de la cr\u00e9ation d&rsquo;un pr\u00e9c\u00e9dent module, pour lequel j&rsquo;ajoutais un champ dans le formulaire dans le formulaire de cr\u00e9ation de compte, j&rsquo;ai pas mal \u00e9t\u00e9 frustr\u00e9 de ne pas pouvoir le positionner ou je voulais dans ce formulaire.Vous pouvez trouver le module ici : Prestashop : Ajouter un champ confirmation email sur [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[104,615],"class_list":["post-6648","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-prestashop","tag-registration"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/6648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=6648"}],"version-history":[{"count":2,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/6648\/revisions"}],"predecessor-version":[{"id":6651,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/6648\/revisions\/6651"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=6648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=6648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=6648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}