# Override hook to modify existing code

**URL:** <https://www.dolibarr.org/forum/t/override-hook-to-modify-existing-code/24199>\
**Category:** Developing for Dolibarr\
**Tags:** Issue\
**Created:** [August 25, 2023, 1:31pm UTC](https://www.dolibarr.org/forum/t/override-hook-to-modify-existing-code/24199 "2023-08-25T13:31:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasha](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/rasha/32/5356_2.png) [@Rasha](https://www.dolibarr.org/forum/u/Rasha)\
**Post date:** [August 25, 2023, 1:31pm UTC](https://www.dolibarr.org/forum/t/override-hook-to-modify-existing-code/24199/1 "2023-08-25T13:31:46Z")

</div>

**I’m trying to use hooks to modify an existing code in Dolibarr, specifically the dashboard of invoices. However, my hook is not being called. I followed the documentation precisely, but it still isn’t working**

```auto
<?php
class ActionsMymodule
{
    /**
     * @var DoliDB Database handler.
     */
    public $db;

    /**
     * @var string Error code (or message)
     */
    public $error = '';

    /**
     * @var array Errors
     */
    public $errors = array();

    /**
     * @var array Hook results. Propagated to $hookmanager->resArray for later reuse
     */
    public $results = array();

    /**
     * @var string String displayed by executeHook() immediately after return
     */
    public $resprints;

    /**
     * @var int Priority of hook (50 is used if value is not defined)
     */
    public $priority;

    /**
     * Constructor
     *
     * @param DoliDB $db Database handler
     */
    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * Overloading the dashboardAccountancy function : replacing the parent's function with the one below
     *
     * @param array $parameters Hook metadatas (context, etc...)
     * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
     * @param string $action Current action (if set). Generally create or edit or null
     * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
     * @return int < 0 on error, 0 on success, 1 to replace standard code
     */
    public function dashboardAccountancy($parameters, &$object, &$action, $hookmanager)
    {
        global $conf, $user, $langs;

        // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
        $hookmanager->initHooks(array('invoiceindex'));

        $error = 0; // Error counter

        /* print_r($parameters); print_r($object); echo "action: " . $action; */
        // if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
        // // Do what you want here...
        // // You can for example call global vars like $fieldstosearchall to overwrite them, or update database depending on $action and $_POST values.
        // }

        if (!$error) {
            $this->results = array('myreturn' => 999);
            $this->resprints = '<script type="text/javascript" language="javascript">
            var element = document.getElementById("id-right");
            element.style.display = "none";</script>';
            return 0; // or return 1 to replace standard code
        } else {
            $this->errors[] = 'Error message';
            return -1;
        }
    }

    /**
     * Overloading the doActions function : replacing the parent's function with the one below
     *
     * @param array $parameters Hook metadatas (context, etc...)
     * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
     * @param string $action Current action (if set). Generally create or edit or null
     * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
     * @return int < 0 on error, 0 on success, 1 to replace standard code
     */
    public function doActions($parameters, &$object, &$action, $hookmanager)
    {
        global $conf, $user, $langs;

        // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
        $hookmanager->initHooks(array('doActions'));

        $error = 0; // Error counter
        /* print_r($parameters); print_r($object); echo "action: " . $action; */
        if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
            // Do what you want here...
            // You can for example call global vars like $fieldstosearchall to overwrite them, or update database depending on $action and $_POST values.
        }

        if (!$error) {
            $this->results = array('myreturn' => 999);
            $this->resprints = 'A text to show';
            return 1; // or return 1 to replace standard code
        } else {
            $this->errors[] = 'Error message';
            return -1;
        }
    }
}

```

```auto
$this->module_parts = array(
			// Set this to 1 if module has its own trigger directory (core/triggers)
			'triggers' => 0,
			// Set this to 1 if module has its own login method file (core/login)
			'login' => 0,
			// Set this to 1 if module has its own substitution function file (core/substitutions)
			'substitutions' => 0,
			// Set this to 1 if module has its own menus handler directory (core/menus)
			'menus' => 0,
			// Set this to 1 if module overwrite template dir (core/tpl)
			'tpl' => 0,
			// Set this to 1 if module has its own barcode directory (core/modules/barcode)
			'barcode' => 0,
			// Set this to 1 if module has its own models directory (core/modules/xxx)
			'models' => 0,
			// Set this to 1 if module has its own printing directory (core/modules/printing)
			'printing' => 0,
			// Set this to 1 if module has its own theme directory (theme)
			'theme' => 1,
			// Set this to relative path of css file if module has its own css file
			'css' => array(
				// '/Mymodule/css/Mymodule.css.php',
			),
			// Set this to relative path of js file if module must load a js on all pages
			'js' => array(
				// '/Mymodule/js/Mymodule.js.php',
			),
			// Set here all hooks context managed by module. To find available hook context, make a "grep -r '>initHooks(' *" on source code. You can also set hook context to 'all'
			'hooks' => array(
				'data' => array(
					'invoiceindex',
				),
				// 'entity' => '0',
			),
			// Set this to 1 if features of module are opened to external users
			'moduleforexternal' => 0,
		);

```

i want to override the “dashboardAccountancy” hook in this file compta\index.php :

```auto
......
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
$hookmanager->initHooks(array('invoiceindex'));
print '</div></div>';
$parameters = array('user' => $user);

.....

$reshook = $hookmanager->executeHooks('dashboardAccountancy', $parameters, $object); // Note that $action and $object may have been modified by hook

// End of page
llxFooter();

$db->close();

```

---

<div class="post-metadata">

**Author:** ![ksar](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/ksar/32/514_2.png) [@ksar](https://www.dolibarr.org/forum/u/ksar)\
**Post date:** [August 25, 2023, 4:13pm UTC](https://www.dolibarr.org/forum/t/override-hook-to-modify-existing-code/24199/2 "2023-08-25T16:13:55Z")

</div>

Hello,

Without the full code it will be difficult to help but usally it’s a problem of file name

Becarfull that all the module name should be without caps on file name : **/htdocs/_yourmodulename_/class/actions\__yourmodulename_.class.php**

---

<div class="post-metadata">

**Author:** ![Rasha](https://www.dolibarr.org/forum/user_avatar/www.dolibarr.org/rasha/32/5356_2.png) [@Rasha](https://www.dolibarr.org/forum/u/Rasha)\
**Post date:** [August 25, 2023, 4:46pm UTC](https://www.dolibarr.org/forum/t/override-hook-to-modify-existing-code/24199/3 "2023-08-25T16:46:42Z")

</div>

[https://www.dolibarr.org/forum/t/the-new-hook-system/24200/5?u=rasha](https://www.dolibarr.org/forum/t/the-new-hook-system/24200/5)

i forgot to update this post

i previously added this

```auto

> $this->const = array(
> 0 => array(‘MAIN_MODULE_MYMODULE_HOOKS’, ‘chaine’, ‘invoiceindex’, ‘Hooks modifying dash for invoice’, 1),
> );

```

, this entry will be automatically added when ienable the module. i removed it from database, reenable the module and check the entry into llx\_const exists. and its done !.
