Jump to content

Assign variables in php within a hook


Stathinho

Recommended Posts

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');

Link to comment
Share on other sites

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,

		)
		);
	}
});

RQKwRT3.png

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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";

OqU9YKL.png

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";

yDV9k4m.png

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...

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