Stathinho Posted August 22, 2019 Share Posted August 22, 2019 Hello, I am trying to create a hook for adding a new element in sidebar. I have created the hook. add_hook('ClientAreaPrimarySidebar', 1, function($primarySidebar) { /* @var \WHMCS\View\Menu\Item $primarySidebar */ $newMenu = $primarySidebar->addChild( 'accountmanager', array( 'name' => 'Home', 'label' => Lang::trans('account_manager'), 'order' => 1, 'icon' => 'fas fa-life-ring', ) ); $newMenu->addChild( 'accountmanager', array( 'label' => Lang::trans('account_manager_label'), ) ); }); I am trying somehow to get the userid, and assign it to a variable. And is it possible to be used in translations? I tried with this one here but it's not working: $variable = $template->getVariable('variablename')->value; $template->assign('variablename ', 'variable'); 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted August 22, 2019 Share Posted August 22, 2019 1 hour ago, Stathinho said: I am trying somehow to get the userid, and assign it to a variable. And is it possible to be used in translations? i'm going to assume because you're mentioning userid, that a client would need to be logged in to see this sidebar... <?php use WHMCS\View\Menu\Item as MenuItem; add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) { $client = Menu::context('client'); if ($client) { $newMenu = $primarySidebar->addChild('accountmanager', array( 'name' => 'Home', 'label' => Lang::trans('account_manager'), 'order' => 1, 'icon' => 'fas fa-life-ring', ) ); $newMenu->addChild('accountmanager', array( 'label' => Lang::trans('account_manager_label').$client->id, ) ); } }); now your sidebar will look slightly different because you will have those language strings already created, but in that screenshot, '41' is the user id of the logged in client... the point being that you don't necessarily have to assign it to a variable if all you're going to be doing is outputting it in the label... 0 Quote Link to comment Share on other sites More sharing options...
Stathinho Posted August 23, 2019 Author Share Posted August 23, 2019 Great! Thank you indeed Brian! It's working 🙂 How about, if I want to push the variable into the traslations? For example, if I wanna say: "Your id is $client->id and you I am your account manager" 'label' => Lang::trans('account_manager_label1').$client->id.Lang::trans('account_manager_label2'), I guess, that could be it? By splitting the 2 translations? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted August 24, 2019 Share Posted August 24, 2019 20 hours ago, Stathinho said: How about, if I want to push the variable into the translations? For example, if I wanna say: "Your id is $client->id and you I am your account manager" there are a couple of ways - though if what you're outputting is more than just a label, you're more into the realms of adding a body to the sidebar, rather than creating a new child and label... <?php use WHMCS\View\Menu\Item as MenuItem; add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) { $client = Menu::context('client'); if ($client) { $primarySidebar->addChild('accountmanager', array( 'name' => 'Home', 'label' => Lang::trans('account_manager'), 'order' => 1, 'icon' => 'fas fa-life-ring', ) ); $accountbody = sprintf(Lang::trans('account_manager_label'),$client->id); $primarySidebar->getChild('accountmanager')->setBodyHtml($accountbody); } }); and then the language string used is... $_LANG['account_manager_label'] = "Your id is %s and I am your account manager"; and to answer your next question before you even ask it, yes you can inject multiple variables into the string... 🙂 $accountbody = sprintf(Lang::trans('account_manager_label'),$client->firstName,$client->id); $_LANG['account_manager_label'] = "Hello %s, your id is %s and I am your account manager"; 1 Quote Link to comment Share on other sites More sharing options...
Stathinho Posted August 26, 2019 Author Share Posted August 26, 2019 Not all heroes wear capes!! You are really awesome. ...And a last question. How can I use the {debug} variables in php of a page? In tpl files, It's really easy, as they have the same name. But how do I use them in php? Do I have to assign them somehow? And if yes, can it be done within a hook? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted August 26, 2019 Share Posted August 26, 2019 59 minutes ago, Stathinho said: How can I use the {debug} variables in php of a page? you can output the PHP variables/arrays to the screen with... var_dump(get_defined_vars()); or by printing/echoing a specific variable. if you mean, where the hell did I know to use $client->id and $client->firstName, then they're listed in the class docs.. 🙂 https://classdocs.whmcs.com/7.7/WHMCS/User/Client.html I should also add that you could define your own variables in the hook and use them - I only used the above as they were familiar to me and easily available. 0 Quote Link to comment Share on other sites More sharing options...
miladparsi77 Posted August 9, 2021 Share Posted August 9, 2021 hi how can i have access to the php variables in my hook function?? i want to send my php data to the hook i tried many diffrent ways but they didnt work 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.