Hooks system: Difference between revisions

Tag: 2017 source edit
Tag: 2017 source edit
Line 49: Line 49:
</source>
</source>


Every business always requires something "extra" or different to the Dolibarr core functionality.
''''hookmanager->executeHooks()'''' akzeptiert 4 Parameter und fügt einen Hook hinzu (das ist ein Einstiegspunkt in Ihr Skript für Funktionen, die außerhalb Ihres Skripts und Moduls liegen):
While all improvements and extensions to the Dolibarr code should be contributed back via Github to be included in a future version, some modifications will be too specific to be of interest to all.
Hence the Hooks system was introduced in Dolibarr. This allows developers to add custom code/features to the Dolibarr core files without the need to modify the core files.
This greatly simplifies the upgrading process by keeping modifications separate from the core code.


As opposed to [[triggers]] (actions linked to a business event) − hooks are entry points into the program and can modify any part of the program functions.
- ''''Hookname'''' ist der Name der Methode, die aufgerufen wird. Zum Beispiel: 'formObjectOptions'.
There are many hooks already built into the code and you may add more. They can be considered a point where the code execution is diverted "out" of the current page and runs your code, then returns back.
Your custom code may add something or modify an object or completely replace an existing code block, depending on the implementation of the hook in that specific instance.


Every hook has a name, eg. "doActions" found in this code: $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action);
- ''''$parameters'''' ist ein benutzerdefiniertes Array, um mehr benutzerdefinierte Daten an den Hook zu übermitteln (die Funktion im Hook kann diese Daten verarbeiten). Platzieren Sie hier, was Sie wollen, es kann eine Datei, ein Array mit Zeichenketten oder etwas anderes sein. Zum Beispiel:
However the hook name is often not unique (may be repeated in various code modules), so to identify the location accurately, code modules have a "context" name that will be referred to by all hooks in that code module location.
So to use a hook, you need to know its context name (its location) and the hook name.


*The Hook Context Name (e.g.: "productcard" for products, "invoicecard" for invoices, etc..). can be found by searching for "'''initHooks('''" in the php files.
<source lang=php>
$parameters=array('file'=>'my/path/to/a/file', 'customnames'=>array('henry','david','john'));
</source>


This will find results such as "$hookmanager->initHooks(array('thirdpartycomm','globalcard'));" where "thirdpartycomm" is the context.
- '''$object''' ist das Objekt, das Sie der Hook-Funktion übergeben wollen, sicherlich die Daten des aktuellen Moduls (z. B. das Rechnungsobjekt, wenn Sie sich in einem Rechnungsmodul befinden, usw.). Es kann alles Mögliche sein, aber denken Sie daran, dass es die Hauptkomponente ist, die von den Hook-Funktionen verwendet wird.


*The Hook Name can be found by searching for "'''executeHooks('''" in the block of code you are interest in modifying...possibly there may be no hook defined so you may add your own (and submit a corresponding pull-request in Github so it gets included in a future version).
- '''$action''' ist ein String, der die aktuelle Aktion angibt (kann null sein oder etwas, das wie "create" oder "edit" aussieht).


This will find results such as "$reshook = $hookmanager->executeHooks('addMoreBoxStatsCustomer', $parameters, $object, $action);" where "addMoreBoxStatsCustomer" is the Hook name.
Hinweis: Sie müssen diesen Schritt mehrmals wiederholen, wenn Sie mehrere Hooks an verschiedenen Stellen Ihres Skripts einfügen wollen.
 
Nun sollte Ihr Modul gehookt werden können. Sie können die unten unter '''Hook implementieren''' beschriebene Vorgehensweise befolgen, um eine Hook-Funktion zu implementieren, die den Vorteil dieser Funktion nutzt (Sie können auch testen, ob es funktioniert). 
 
==Einen Hook implementieren==
Um einen Hook zu verwenden (also einen Teil des Codes hinzuzufügen oder zu überschreiben), müssen Sie zunächst einen Moduldeskriptor definiert haben (siehe [[Développement module|Modul_Entwicklung#Modul_Deskriptor_erstellen_(erforderlich)]] dafür).
Anschließend müssen Sie die folgenden Schritte durchführen:
 
1. Fügen Sie Ihr Modul dem Kontext hinzu, in dem der Hook ausgeführt werden soll. Das heißt, wenn man sich in dem gegebenen Kontext befindet, wird Ihr Code aufgerufen. Bearbeiten Sie dazu den Deskriptor Ihres Moduls ('''/htdocs/yourmodulename/core/modules/modYourModuleName.class.php''') und füllen Sie die Variable '''$this->module_parts''' wie im Beispiel :
 
<source lang=php>
$this->module_parts = array(
'hooks' => array('hookcontext1','hookcontext2')  // Set here all hooks context you want to support
);
</source>
 
Anmerkung: Sie können den Kontext eines Moduls auch finden, indem Sie Folgendes hinzufügen
 
<source lang=php>
print('Module context: '.$object->context);
</source>
(Fügen Sie diesen Code in die php-Datei ein, in der die Hooks aufgerufen werden, und löschen Sie ihn wieder, sobald der Wert des Kontexts ermittelt wurde).
 
[[Datei:warning.png]] Achtung: Vergessen Sie nicht, Ihr Modul in der Modulverwaltung zu deaktivieren und dann wieder zu aktivieren, damit die Änderung wirksam wird, denn die Speicherung der zu verwaltenden "Modul-Hooks"-Paare erfolgt in der Datenbank, die erst bei der Aktivierung des Moduls aktualisiert wird.


=Adding a hook point into code=
=Adding a hook point into code=
265

edits