Jump to content

Hook to change all the secondarysidebar icons links all together


plusplushosting

Recommended Posts

Hi, im trying to use this hook which is changing only one of the links icon, but is there any way to change all the links with few lines or i have to create a function for every one?

 

add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar)
{

    if (!is_null($secondarySidebar->getChild('itemname'))) {
             $secondarySidebar->getChild('itemname')
			 	->getChild('A Records')
				->setIcon('fad fa-pencil');

					
    }
});

Thanx in advance!

Link to comment
Share on other sites

12 hours ago, plusplushosting said:

Hi, im trying to use this hook which is changing only one of the links icon, but is there any way to change all the links with few lines or i have to create a function for every one?

it's doable...

and that code could be trimmed down further by using an associative array.

but per your original hook, once you know how to change an icon for one child, then it's just copy & pasting the code (e.g multiple f statements in the same hook file), changing the menuitemname and icon references for each child - so if you're happier doing that, then it will work too.

Link to comment
Share on other sites

Thanx very much Brian!

I worked it out with your help and that code!

Just another thing now is conflicting...based in the code i posted above.....in certain menues is happening that the child "A Records" doesn't exist, so is trowing me error (ooopps) ...should i put an IF there? I have attempted but looks like im not finding the correct one 😞

Thanx! Have a great weekend!

Link to comment
Share on other sites

I have modified your code but i havent got it working 😞

 

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar)
{
    	$ActionDetails = $primarySidebar->getChild("Domain Details Management");
	     if (empty($ActionDetails)) {
		return;
	     }
	    $ActionDetailsChildren = $ActionDetails->getChildren();
		$kidsToIcon = ["Domain Forwarding"];
	    foreach($ActionDetailsChildren as $key => $Action_details_child) {
		if (in_array($key, $kidsToIcon)) {
			if ($key === "Domain Forwarding"){$newicon = "fas fa-ticket ls ls-pen";}
			$ActionDetails->getChild($key)->setIcon($newicon);
		}
	}
	
});

 

Link to comment
Share on other sites

12 hours ago, plusplushosting said:

Just another thing now is conflicting...based in the code i posted above.....in certain menus is happening that the child "A Records" doesn't exist, so is throwing me error (ooopps) ...should i put an IF there?

if you enable Display Errors in general settings -> other, what's the exact error ?

my suspicion is that whatever is adding "Domain Forwarding" & "A Records" links to the sidebar - and i'm assuming it's a registrar addon (as they aren't default sidebar entries) is running after this hook... though i'll know more based on what the error is.

Link to comment
Share on other sites

Hi Brian!

Sorry for the half info....i don't know why you are not using your crystal ball 🙂

Lets start over as im mixing almost everything at this point.

yes, the code is a domain management addon and it insert the menu on certain situations on certain domains....so i guess when the link of that icon which is attempting to change is not there, is when is trowing the error. So i guess before the

->getChild('Domain Forwarding')

line should be an IF to first check if that link is there.

 

I mixed up things i guess...my original code to change the icon was:

<?php

#remove some links from sidebar and change a domain forwarding link icon
use WHMCS\View\Menu\Item as MenuItem;
 
add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar)
{

    if (!is_null($primarySidebar->getChild('Domain Details Management'))) {
             $primarySidebar->getChild('Domain Details Management')
                            ->removeChild('Get EPP Code')
			    ->getChild ('Domain Forwarding')
			    ->setIcon ('fas fa-ticket ls ls-pen');

						
    }
});

But then when you have  provided me the link to the code to change the secondarsidebar icons using foreach, i have attempted to use that code (the one in the 4th post) to change the icon in the primaysidebar...and as you can see and guess, without luck, hahaha

btw, i have many functions in the same hook file for the same sidebar, is ok? simply changing the priority number on each is the correct way? I mean, to not conflict each other. As far as i can see is working ok, unless i do add those 2 last lines in the function above.

Ahh btw...this is the error....i have renamed the sensitive info

Error: Call to a member function setIcon() on null in /home/username/public_html/whmcsdir/templates/themename/core/hooks/hookfile.php:15
Stack trace:
#0 /home/username/public_html/whmcsdir/includes/hookfunctions.php(0): RSThemes\Service\Hooks->{closure}(Object(WHMCS\View\Menu\Item))
#1 /home/username/public_html/whmcsdir/vendor/whmcs/whmcs-foundation/lib/ClientArea.php(0): run_hook('ClientAreaPrima...', Array, true)
#2 /home/username/public_html/whmcsdir/vendor/whmcs/whmcs-foundation/lib/ClientArea.php(0): WHMCS\ClientArea->outputWithoutExit()
#3 /home/username/public_html/whmcsdir/vendor/whmcs/whmcs-foundation/lib/Http/Message/AbstractViewableResponse.php(0): WHMCS\ClientArea->getOutputContent()
#4 /home/username/public_html/whmcsdir/vendor/zendframework/zend-diactoros/src/Response/SapiEmitterTrait.php(27): WHMCS\Http\Message\AbstractViewableResponse->getBody()
#5 /home/username/public_html/whmcsdir/vendor/zendframework/zend-diactoros/src/Response/SapiEmitter.php(34): Zend\Diactoros\Response\SapiEmitter->injectContentLength(Object(WHMCS\ClientArea))
#6 /home/username/public_html/whmcsdir/clientarea.php(0): Zend\Diactoros\Response\SapiEmitter->emit(Object(WHMCS\ClientArea))
#7 {main}

Thanx!

Link to comment
Share on other sites

23 hours ago, plusplushosting said:

Sorry for the half info....i don't know why you are not using your crystal ball 🙂

frustratingly, the damn thing never works at the weekend - I must have bought a dodgy one.. 🔮

23 hours ago, plusplushosting said:

Lets start over as im mixing almost everything at this point.

not what I like to hear when people are writing hooks. 😲

23 hours ago, plusplushosting said:

So i guess before the ->getChild('Domain Forwarding') line should be an IF to first check if that link is there.

that's the golden rule when modifying a menu with a hook - you have to check that it exists before trying to modify it.

23 hours ago, plusplushosting said:

I mixed up things i guess...my original code to change the icon was:

which only checks that the sidebar exists, not whether the children in there that you want to remove exist before you try to remove them.

23 hours ago, plusplushosting said:

But then when you have  provided me the link to the code to change the secondarysidebar icons using foreach, i have attempted to use that code (the one in the 4th post) to change the icon in the primarysidebar...and as you can see and guess, without luck, hahaha

even if you change its priority from 1 to something much larger, e.g 20 ? that should give the addon plenty of time to add the link, and for your hook to modify it.

for me, the hook does nothing - obviously, the Forwarding link doesn't exist, but because it's not in the sidebar array, the hook ignores it and doesn't try to modify it... how sure are you that it's this foreach hook that's causing the issue ?

23 hours ago, plusplushosting said:

btw, i have many functions in the same hook file for the same sidebar, is ok?

it's ok - so long as either every hook is checking conditions correctly, or the code is in a logical order,e.g you're not modifying something before you're adding it.

23 hours ago, plusplushosting said:

simply changing the priority number on each is the correct way?

they don't need to be separate hooks with differing priorities in the same file, e.g you don't need to have 20 separate primarysidebar hooks in the same file - you can do it, but that often leads to confused ordering...

btw - it's often worth mentioning if you're using a custom client theme as that can have an impact on hooks... i'm assuming this is a customised Lagom theme? the hook should work on that too without issue as i've just tried it.

Link to comment
Share on other sites

hahaha....that crystal ball is asking for vacations i guess 🙂

4 hours ago, brian! said:

for me, the hook does nothing - obviously, the Forwarding link doesn't exist, but because it's not in the sidebar array, the hook ignores it and doesn't try to modify it... how sure are you that it's this foreach hook that's causing the issue ?

I wasnt aware that ignores it if doesnt exist....every day i learn something else , you are a source of wisdom 🙂

4 hours ago, brian! said:

btw - it's often worth mentioning if you're using a custom client theme as that can have an impact on hooks... i'm assuming this is a customised Lagom theme? the hook should work on that too without issue as i've just tried it.

yeah, sometimes i assume stuff which probably i have to mention.

Ill try to remove all the other hook files in case any of them is breaking this and see what happen. But is very odd, just adding those two lines is erroring, without them everything is working as expected or at least is not erroring (which is not the same).

Thanx again!

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • 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