Jump to content

Problem with hooks


Recommended Posts

Hi, 
I'm writing this post because i have problems when i use my hook.
I have tested all the things we can do with hooks and I have the same ending and a similar errors . 
For an exemple and show you what was the problem, I took the hook who can move down an child item.
 That doesn't work corretly and the site crashes.
I'm putting here the error message :

Error: Call to a member function getChild() on null in /var/www/html/includes/hooks/testhook.php:6
Stack trace:
#0 /var/www/html/vendor/whmcs/whmcs-foundation/lib/Hook/Manager.php(0): WHMCS\Utility\SafeInclude::{closure}()
#1 /var/www/html/vendor/illuminate/support/Facades/Facade.php(261): WHMCS\Hook\Manager->run()
#2 /var/www/html/includes/functions.php(0): Illuminate\Support\Facades\Facade::__callStatic()
#3 /var/www/html/vendor/whmcs/whmcs-foundation/lib/ClientArea.php(0): run_hook()
#4 /var/www/html/vendor/whmcs/whmcs-foundation/lib/ClientArea.php(0): WHMCS\ClientArea->outputWithoutExit()
#5 /var/www/html/vendor/whmcs/whmcs-foundation/lib/Http/Message/AbstractViewableResponse.php(0): WHMCS\ClientArea->getOutputContent()
#6 /var/www/html/vendor/laminas/laminas-httphandlerrunner/src/Emitter/SapiEmitter.php(41): WHMCS\Http\Message\AbstractViewableResponse->getBody()
#7 /var/www/html/vendor/laminas/laminas-httphandlerrunner/src/Emitter/SapiEmitter.php(31): Laminas\HttpHandlerRunner\Emitter\SapiEmitter->emitBody()
#8 /var/www/html/clientarea.php(0): Laminas\HttpHandlerRunner\Emitter\SapiEmitter->emit()
#9 {main}

I followed this doc https://developers.whmcs.com/themes/navigation/
I'm entering correctly the name of menu items but I don't know why that doesn't works.
Here is my code if somebody could help me :

use WHMCS\View\Menu\Item as MenuItem;
 
add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar)
{
    $primarySidebar->getChild('Account')->getChild('Edit Account Details')->moveUp();
});

 

Link to comment
Share on other sites

  • 2 months later...
$account = $primarySidebar->getChild('Account');
if (!is_null($account)) {
    $accountDetails = $account->getChild('Edit Account Details');
    if (!is_null($account)) {
        $accountDetails->moveUp();
    }
}

Somewhat late, but it will be useful for history. You must check for the existence of objects before calling them.

Link to comment
Share on other sites

1 hour ago, LosBooom said:

$account = $primarySidebar->getChild('Account');
if (!is_null($account)) {
    $accountDetails = $account->getChild('Edit Account Details');
    if (!is_null($account)) {
        $accountDetails->moveUp();
    }
}

Somewhat late, but it will be useful for history. You must check for the existence of objects before calling them.

In addition to this reply, you need to check the correct  Menu Name (documentation shows how).

If you are trying to getChild() of a menu name that doesn’t exist, you will get the mentioned error (of null).

You need to start from parent, then to child.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated