Line 132:
Line 132:
== Implémentation en code php (module core Dolibarr ou pour vos propres modules) ==
== Implémentation en code php (module core Dolibarr ou pour vos propres modules) ==
−
One of the main features of the CustomFields module is that it offers a generic way to access, add, edit and view custom fields from your own code. You can easily develop your own modules accepting user's inputs based on CustomFields.
+
Une des fonctionnalités principales du module CustomFields est qu'il offre un moyen générique d'accéder, d'ajouter, de modifier et d'afficher des champs personnalisés depuis votre propre code. Vous pouvez facilement développer votre propre module en utilisant uniquement des champs basés sur la classe CustomFields.
−
You can use a simplifier library that eases a lot the usage of custom fields in php codes:
+
Pour récupérer les valeurs des champs, vous pouvez utiliser la librairie simplificatrice qui facilite beaucoup l'utilisation des champs personnalisés vos codes php:
<source lang="php">
<source lang="php">
dol_include_once('/customfields/lib/customfields_aux.lib.php'); // include the simplifier library
dol_include_once('/customfields/lib/customfields_aux.lib.php'); // include the simplifier library
$customfields = customfields_fill_object($object, null, $langs); // load the custom fields values inside $object->customfields
$customfields = customfields_fill_object($object, null, $langs); // load the custom fields values inside $object->customfields
+
</source>
+
+
Vous pouvez alors facilement accéder aux valeurs des champs personnalisés comme ceci:
+
<source lang="php">
+
print($object->customfields->cf_myfield);
+
</source>
+
+
Pour charger les champs personnalisés des lignes produits, vous pouvez utiliser la fonction customfields_fill_object_line():
+
<source lang="php">
+
dol_include_once('/customfields/lib/customfields_aux.lib.php'); // include the simplifier library
+
$customfields = customfields_fill_object_lines($object, null, $langs); // load the custom fields values inside $object->customfields
+
</source>
+
+
Vous pouvez alors accéder aux champs des lignes produits comme ceci:
+
<source lang="php">
+
$object->customfields->lines->$lineid->cf_myfield
+
</source>
+
+
Vous pouvez également obtenir (et bien plus) manuellement les valeurs des champs personnalisés en utilisant la classe CustomFields:
+
+
<source lang="php">
+
// Init and main vars
+
//include_once(DOL_DOCUMENT_ROOT.'/customfields/class/customfields.class.php'); // OLD WAY
+
dol_include_once('/customfields/class/customfields.class.php'); // NEW WAY since Dolibarr v3.3
+
$customfields = new CustomFields($this->db, $currentmodule); // where $currentmodule is the current module, you can replace it by '' if you just want to use printing functions and fetchAny.
+
+
//$records = $customfields->fetchAll(); // to fetch all records
+
$records = $customfields->fetch($id); // to fetch one object's records
</source>
</source>