Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with HookManager Not Propagating results Correctly in Hooks #31295

Open
diegoserranobst opened this issue Oct 6, 2024 · 1 comment
Open
Labels
Bug This is a bug (something does not work as expected)

Comments

@diegoserranobst
Copy link

Bug

There seems to be an issue with the propagation of the results variable within the HookManager class in Dolibarr, specifically when using the menuLeftMenuItems method in custom hooks. The $hookmanager->results are not automatically propagated to $actionclassinstance->results after the hook execution, which forces a manual assignment for the desired result to take effect.

The following workaround (manual result assignment) solves the issue temporarily, but this behavior seems unintended:

// Manual result propagation workaround
if (!empty($this->results)) {
    $actionclassinstance->results = $this->results;
}

This manual propagation should not be necessary and suggests a bug in how HookManager handles the hook execution process.

Workaround:
o temporarily resolve the issue, I’ve had to manually force the propagation of results by adding this snippet in hookmanager.class.php:

if (!empty($this->results)) {
    $actionclassinstance->results = $this->results;
}

Dolibarr Version

20.0.0

Environment PHP

8.2

Environment Database

pgsql

Steps to reproduce the behavior and expected behavior

Create a custom hook for modifying the left menu, specifically using menuLeftMenuItems.
Assign new menu items to $hookmanager->results.
Observe that the menu does not reflect the changes unless you manually assign $hookmanager->results to $actionclassinstance->results.
Code Snippet:
Here is the hook implementation:

function menuLeftMenuItems($parameters, &$object, &$action, &$hookmanager) {
    // Log the start of the modification
    dol_syslog("Hook: Starting menu modification", LOG_DEBUG);

    // Modify the menu by adding a new item
    $object[] = array(
        'url' => '/custom/new_option.php',
        'titre' => 'New Option',
        'level' => 0,
        'enabled' => 1,
    );
   
    // Assign changes to hookmanager
    $hookmanager->results = array_values($object);

    dol_syslog("After assigning to hookmanager->results: " . var_export($hookmanager->results, true), LOG_DEBUG);

    return 1;  // Replace the entire menu
}

Attached files

No response

@diegoserranobst diegoserranobst added the Bug This is a bug (something does not work as expected) label Oct 6, 2024
@lippoliv
Copy link
Contributor

I also had some issues with menuLeftMenuItems and debugged. You shouldn't update $hookmanager->results but $this->results. It'll work then.

This code works without any changes.

        /**
         * @throws \Exception
         * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection
         * @noinspection PhpUnusedParameterInspection
         * @noinspection PhpUnused
         */
        public function menuLeftMenuItems(
            array       $parameters,
            array       $items,
            string      $action,
            HookManager $hookmanager
        ): int {
            $items[] = [
                "classname" => "",
                "enabled"   => 1,
                "id"        => "",
                "idsel"     => "",
                "leftmenu"  => "",
                "level"     => 0,
                "mainmenu"  => "",
                "position"  => 0,
                "prefix"    => "",
                "target"    => "",
                "titre"     => "My menu",
                "url"       => "#",
            ];

            $this->results = $items;

            return 1;
        }

What I couldn't make work is just "append" without replace, this in theory is possible by returning 0, here's my try

        /**
         * @throws \Exception
         * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection
         * @noinspection PhpUnusedParameterInspection
         * @noinspection PhpUnused
         */
        public function menuLeftMenuItems(
            array       $parameters,
            array       $items,
            string      $action,
            HookManager $hookmanager
        ): int {
            $this->results = [
                [
                    "classname" => "",
                    "enabled"   => 1,
                    "id"        => "",
                    "idsel"     => "",
                    "leftmenu"  => "",
                    "level"     => 0,
                    "mainmenu"  => "",
                    "position"  => 0,
                    "prefix"    => "",
                    "target"    => "",
                    "titre"     => "My menu",
                    "url"       => "#",
                ],
            ];

            return 0;
        }

I guess it doesn't work because the array is added as one item to the menu list, instead of being merged into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This is a bug (something does not work as expected)
Projects
None yet
Development

No branches or pull requests

2 participants