Module CustomFields FR
L'article n'a pas encore été entièrement traduit en français.
Veuillez lire le wiki en anglais qui est déjà complet (icône à gauche).
Traduction du libellé d'un champ
Les champs peuvent être facilement renommé ou traduit dans plusieurs langues en éditant les fichiers de langues.
Ouvrez le fichier /customfields/langs/code_CODE/customfields-user.lang (où code_CODE est le code ISO de votre région, ex: en_US ou fr_FR) et ajoutez dedans le nom de la Variable de votre champ personnalisé (affiché dans le panneau administrateur, colonne Variable) suivi de la traduction (format: cf_monchamp= Mon Libellé).
Ex: disons que votre champ personnalisé est nommé "user_ref", et que le nom de Variable résultat est "cf_user_ref". Dans customfields-user.lang il vous suffit d'ajouter:
cf_user_ref= Le libellé que vous voulez. Vous pouvez même écrire une très très longue phrase ici.<br />Et vous pouvez même insérer des retours à la ligne avec <br />.
Testez vos champs personnalisés avec le module PDFTest
Un module auxiliare appelé CustomFieldsPDFTest est fourni afin que que vous puissiez facilement et rapidement tester vos champs personnalisés dans vos documents PDF. Cela évite d'avoir à faire votre propre modèle PDF juste pour tester et risquer de faire des erreurs de code php.
Il suffit juste d'activer le module CustomFieldsPDFTest dans Accueil>Configuration>Modules et ensuite de générer un fichier PDF en utilisant n'importe quel modèle.
Une page sera rajouté à la fin du fichier PDF généré, contenant une liste extensive de tous les champs personnalisés disponibles ainsi que leurs valeurs, et leurs valeurs brut(=raw) (valeur raw = pas de beautification, pas d'encode html ni de traduction).
Vous pouvez ainsi vérifier qu'un champ personnalisé correspond bien à vos besoins et délivre toutes les informations dont vous aurez besoin dans votre futur modèle PDF.
Quand vous avez fini le test, désactivez simplement le module, vous ferez votre propre modèle PDF (voir ci-dessous)
Note: les documents PDF déjà générés ne seront pas affectés, seulement les documents générés après l'activation du module PDFTest se verront octroyés cette page supplémentaire de champs personnalisés, et après désactivation du module, si vous générez à nouveau le document PDF, les pages supplémentaires disparaîtrons.
Implementing in ODT templates
Custom fields are automatically loaded for ODT templates.
Just use the shown variable name (Variable column in the configuration page) as a tag enclosed by two braces.
Eg: for a customfield named user_ref, you will get the Variable name cf_user_ref. In your ODT, to get the value of the field, just type
{cf_user_ref}
You can also get the raw value (without any preprocessing) by appending _raw to the variable name:
{cf_user_ref_raw}
There is also full support for constrained fields, so that if you have a constraint on this field, it will automatically fetch all the linked values of the referenced tables and you will be able to use them with tags. Eg: cf_user_ref is constrained on the llx_user table:
{cf_user_ref} = rowid {cf_user_ref_firstname} = firstname {cf_user_ref_user_mobile} = mobile phone etc...
As you can see, you just need to append '_' and the name of the column you want to access to show the corresponding value.
For lines, it works just the same, you just have to put the variable name inside the table that handles the product's lines, between the tags [!-- BEGIN row.lines --] and [!-- END row.lines --]
Note: an interesting use of custom fields is to use a TrueFalseBox with a conditional substitution, eg: with a custom fields cf_enablethis:
[!-- IF {cf_enablethis_raw} --] This is enabled and this will show. [!-- ELSE {cf_enablethis_raw} --] Else this will show up when disabled. [!-- ENDIF {cf_enablethis_raw} --]
We need to use the raw value, because we need to have a 0/1 value for the conditional to work (or empty/non-empty, so this can also work for empty Textbox: if there's no text, you can avoid to show anything - this in fact works for any other sql datatypes).
Implementing in PDF templates
To use custom fields in your PDF template, you first need to load the custom fields datas, then you can use them wherever you want.
- To load the custom fields:
// Init and main vars for CustomFields
dol_include_once(DOL_DOCUMENT_ROOT.'/customfields/lib/customfields_aux.lib.php');
// Filling the $object with customfields (you can then access customfields by doing $object->customfields->cf_yourfield)
$this->customfields = customfields_fill_object($object, null, $outputlangs, null, true); // beautified values
$this->customfields_raw = customfields_fill_object($object, null, $outputlangs, 'raw', null); // raw values
$this->customfields_lines = customfields_fill_object_lines($object, null, $outputlangs, null, true); // product lines' values
Note: you can place this just after:
$pdf=pdf_getInstance($this->format);
- To access the field's value:
Beautified formatting:
$object->customfields->cf_myfield
or for the raw value:
$object->customfields->raw->cf_myfield
- To access a product's line's value:
$lineid = $object->lines[$i]->rowid;
$object->customfields->lines->$lineid>cf_myfield
Where $lineid must be replaced by the id of the line you want to fetch (rowid of the product, so it does NOT necessary start at 0).
- To print it with FPDF (the default PDF generation library):
$pdf->MultiCell(0,3, $object->customfields->cf_myfield, 0, 'L'); // printing the customfield
- And if you want to print the multilanguage label of this field :
$outputlangs->load('customfields-user@customfields');
$mylabel = $customfields->findLabel("cf_myfield", $outputlangs); // where $outputlangs is the language the PDF should be outputted to
or if you want to do it automatically (useful for a loop):
$outputlangs->load('customfields-user@customfields');
$keys=array_keys(get_object_vars($object->customfields));
$mylabel = $outputlangs->trans($keys[xxx]); // where xxx is a number, you can iterate foreach($keys as $key) if you prefer
Implementing in php code (dolibarr core modules or your own module)
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.
You can use a simplifier library that eases a lot the usage of custom fields in php codes:
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