Hooks system: Difference between revisions

Jump to navigation Jump to search
→‎Implementation: Added $deleteonunactive info thank's to LaMakina + format of a const declaration
No edit summary
(→‎Implementation: Added $deleteonunactive info thank's to LaMakina + format of a const declaration)
Line 16: Line 16:


<source lang="php">
<source lang="php">
$this->const = array( 0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0);
$this->const = array(
0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0, 'current', 1)
);
</source>
</source>
Note: format of a const declaration:
<source lang="php">
$key=>array($const_name,$type,$value,$description,$visible_on_admin_panel=0,$entity='current',$deleteonunactive=0),
$key2=>...
</source>
With $key being a positive number.


Of course change YourModuleName with your own module's name.
Of course change YourModuleName with your own module's name.
Line 23: Line 31:
Then you get the value at the third index, which is a string with contexts separated by a comma.
Then you get the value at the third index, which is a string with contexts separated by a comma.


'''IMPORTANT''': Be careful: do not forget to DISABLE then RENABLE the module in the admin panel to accept the new const values, because these constants are only added to the database when enabling the module.
'''IMPORTANT''': Be careful: do not forget to DISABLE then RENABLE the module in the admin panel to accept the new const values, because these constants are only added to the database when enabling the module. And please note that the const is updated because the last parameter is set to 1 (which represent $deleteonunactive) which by default is set to 0 to preserve the constants even when the module is disabled.


And please note that the const are updated because there is the remove() function (in the same file) that tells Dolibarr to remove the constants when disabling the module, else Dolibarr would not update the constants if they were not removed first (even if the values changed). Your remove() function should be something similar to this:
<source lang="php">
/**
* Function called when module is disabled.
* Remove from database constants, boxes and permissions from Dolibarr database.
* Data directories are not deleted.
* @return    int            1 if OK, 0 if KO
*/
function remove()
{
$sql = array("DELETE FROM ".MAIN_DB_PREFIX."const WHERE name like '%_mymodulename' OR name like '%_mymodulename_%';");
return $this->_remove($sql);
}
</source>


2- You will overload a hook (a function) with your own.
2- You will overload a hook (a function) with your own.
439

edits

Navigation menu