Extrafields: Difference between revisions
(Ajout d'une partie vidéo concernant les extrafields. Not in the language of the page but it's better than nothing or it can help) Tag: 2017 source edit |
(Adding description about "unique" property) |
||
| Line 9: | Line 9: | ||
=Extrafields= | =Extrafields= | ||
Extrafields allow to add others datas to Dolibarr standard database schema. Introduced in version 3.2, extrafields are now implemented in most business objects : | Extrafields allow to add others datas to Dolibarr standard database schema. Introduced in version 3.2, extrafields are now implemented in most business objects<span> </span>: | ||
= Technical implementation = | *Thirdparties & contacts | ||
*Users | |||
*Events | |||
*Proposal | |||
*Customer Invoices | |||
*Orders | |||
*Supplier Proposal | |||
*Supplier Invoices | |||
*Supplier Orders | |||
*Products & services | |||
*Member & member type | |||
*Projects & project tasks (since 3.4) | |||
*Categories | |||
*Expense Report | |||
=Technical implementation= | |||
Each object has its table in database to store values of extrafields. | Each object has its table in database to store values of extrafields. | ||
== Datatables extrafield tables models == | ==Datatables extrafield tables models== | ||
llx_''BaseTableNAme''_extrafields | llx_''BaseTableNAme''_extrafields | ||
| Line 38: | Line 39: | ||
An example of mysql definition file for extrafields implementation on an object : | An example of mysql definition file for extrafields implementation on an object<span> </span>: | ||
create table llx_place_room_extrafields | create table llx_place_room_extrafields | ||
| Line 48: | Line 49: | ||
) ENGINE=innodb; | ) ENGINE=innodb; | ||
= Usage = | The "Unique" property of extrafield create a `UNIQUE INDEX` on this field, forcing objects to have a unique value for their extrafield (two objects cannot have the same value). | ||
== Load class for extrafields == | |||
=Usage= | |||
==Load class for extrafields== | |||
To use extrafields all pages need include | To use extrafields all pages need include | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
| Line 55: | Line 58: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Load extrafield array in object == | ==Load extrafield array in object== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$extrafields = new ExtraFields($db); | $extrafields = new ExtraFields($db); | ||
| Line 64: | Line 67: | ||
This code must be placed into the class for example into the function "write_file". | This code must be placed into the class for example into the function "write_file". | ||
== Load extrafield into object == | ==Load extrafield into object== | ||
Extrafields are loaded in $object->array_options array. | Extrafields are loaded in $object->array_options array. | ||
| Line 75: | Line 78: | ||
This code must be placed just after the above. | This code must be placed just after the above. | ||
== Display extrafield value into PDF or HTML == | ==Display extrafield value into PDF or HTML== | ||
After load extrafields | After load extrafields | ||
| Line 90: | Line 93: | ||
Where XXX is the code of the extrafields | Where XXX is the code of the extrafields | ||
A dedicated page to show how to integrate extrafileds into PDF can be found there : [[Add Extrafields on PDF Models]] | A dedicated page to show how to integrate extrafileds into PDF can be found there<span> </span>: [[Add Extrafields on PDF Models]] | ||
== Display extrafield value of lines into PDF or HTML == | ==Display extrafield value of lines into PDF or HTML== | ||
After load extrafields | After load extrafields | ||
| Line 108: | Line 111: | ||
== How to change the labels of the columns when using models (propals, etc.) == | ==How to change the labels of the columns when using models (propals, etc.)== | ||
In the PHP code of the model you can change the labels of your extrafield by changing into the function named "_tableau(...)" | In the PHP code of the model you can change the labels of your extrafield by changing into the function named "_tableau(...)" | ||
== Before call $object->create() or $object->update() == | ==Before call $object->create() or $object->update()== | ||
Values of extrafields sent by form need to be retrieve. For doing that, use this method : | Values of extrafields sent by form need to be retrieve. For doing that, use this method<span> </span>: | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$ret = $extrafields->setOptionalsFromPost($extralabels,$object); | $ret = $extrafields->setOptionalsFromPost($extralabels,$object); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Inside edit page to display extrafields == | ==Inside edit page to display extrafields== | ||
| Line 129: | Line 132: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Inside view page to display extrafields == | ==Inside view page to display extrafields== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
| Line 139: | Line 142: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== After fetch method of this object class == | ==After fetch method of this object class== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'); | require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'); | ||
| Line 149: | Line 152: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==After Update method of this object class :== | ==After Update method of this object class<span> </span>:== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
// Actions on extra fields (by external module or standard code) | // Actions on extra fields (by external module or standard code) | ||
| Line 170: | Line 173: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== After delete method of this object class == | ==After delete method of this object class== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
// Removed extrafields | // Removed extrafields | ||
| Line 187: | Line 190: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Admin pages == | ==Admin pages== | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
$elementtype='facture'; //Must be the $table_element of the class that manage extrafield | $elementtype='facture'; //Must be the $table_element of the class that manage extrafield | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Repair Database = | =Repair Database= | ||
Search and find definition of $listofmodulesextra add the new extrafield declaration | Search and find definition of $listofmodulesextra add the new extrafield declaration | ||
<syntaxhighlight lang="PHP"> | <syntaxhighlight lang="PHP"> | ||
| Line 201: | Line 204: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Related videos = | =Related videos= | ||
Extrafields: | Extrafields: | ||
Revision as of 19:28, 10 November 2025
Extrafields
Extrafields allow to add others datas to Dolibarr standard database schema. Introduced in version 3.2, extrafields are now implemented in most business objects :
- Thirdparties & contacts
- Users
- Events
- Proposal
- Customer Invoices
- Orders
- Supplier Proposal
- Supplier Invoices
- Supplier Orders
- Products & services
- Member & member type
- Projects & project tasks (since 3.4)
- Categories
- Expense Report
Technical implementation
Each object has its table in database to store values of extrafields.
Datatables extrafield tables models
llx_BaseTableNAme_extrafields rowid integer AUTO_INCREMENT PRIMARY KEY, tms timestamp, fk_object integer NOT NULL, import_key varchar(14)
An example of mysql definition file for extrafields implementation on an object :
create table llx_place_room_extrafields ( rowid integer AUTO_INCREMENT PRIMARY KEY, tms timestamp, fk_object integer NOT NULL, import_key varchar(14) -- import key ) ENGINE=innodb;
The "Unique" property of extrafield create a `UNIQUE INDEX` on this field, forcing objects to have a unique value for their extrafield (two objects cannot have the same value).
Usage
Load class for extrafields
To use extrafields all pages need include
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
Load extrafield array in object
$extrafields = new ExtraFields($db);
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
$extralabels will be an array with code and label of extrafields. This code must be placed into the class for example into the function "write_file".
Load extrafield into object
Extrafields are loaded in $object->array_options array.
$object->fetch($rowid);
$object->fetch_optionals($rowid,$extralabels);
This code must be placed just after the above.
Display extrafield value into PDF or HTML
After load extrafields
HTML Exemple
print $object->array_options ['options_XXX'];
Where XXX is the code of the extrafields
PDF example
$pdf->MultiCell (0,5, $outputlangs->convToOutputCharset($object->array_options ['options_XXX']),0,'L'); )
Where XXX is the code of the extrafields
A dedicated page to show how to integrate extrafileds into PDF can be found there : Add Extrafields on PDF Models
Display extrafield value of lines into PDF or HTML
After load extrafields
HTML Exemple
print $object->lines[$i]->array_options['options_XXX'];
Where XXX is the code of the extrafields
PDF example
$pdf->MultiCell (0,5, $outputlangs->convToOutputCharset($object->lines[$i]->array_options['options_XXX']),0,'L');
Where XXX is the code of the extrafields
How to change the labels of the columns when using models (propals, etc.)
In the PHP code of the model you can change the labels of your extrafield by changing into the function named "_tableau(...)"
Before call $object->create() or $object->update()
Values of extrafields sent by form need to be retrieve. For doing that, use this method :
$ret = $extrafields->setOptionalsFromPost($extralabels,$object);
Inside edit page to display extrafields
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
print $object->showOptionals($extrafields,'edit');
}
Inside view page to display extrafields
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
print $object->showOptionals($extrafields);
}
After fetch method of this object class
require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
$extrafields=new ExtraFields($this->db);
$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
if (count($extralabels)>0) {
$this->fetch_optionals($this->id,$extralabels);
}
After Update method of this object class :
// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('HookModuleNamedao'));
$parameters=array('socid'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
After delete method of this object class
// Removed extrafields
if (! $error)
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->deleteExtraFields();
if ($result < 0)
{
$error++;
dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
}
}
}
Admin pages
$elementtype='facture'; //Must be the $table_element of the class that manage extrafield
Repair Database
Search and find definition of $listofmodulesextra add the new extrafield declaration
$listofmodulesextra=array('societe'=>'societe','adherent'=>'adherent','product'=>'product',
'socpeople'=>'socpeople', 'commande'=>'commande', 'facture'=>'facture',
'commande_fournisseur'=>'commande_fournisseur', 'actioncomm'=>'actioncomm',
'adherent_type'=>'adherent_type','user'=>'user','projet'=>'projet', 'projet_task'=>'projet_task');
Related videos
Extrafields: (vocal in french but subtitles are available and review in this language)