Jump to content

Renaming items in a sidebar


HostingMe

Recommended Posts

Hi All,

I'm using a module that adds integration to a VPS supplier and their module adds controls for a VM in the product details primary sidebar. I'm looking at renaming the labels on these controls and add a font awesome icon. Using the hook below everything works as expected on a VPS product details page however if you try and navigate to a different product type, like shared hosting for example, you're presented with the following error: Error: Call to a member function setLabel() on null in /home/domain/dev.domain.com/includes/hooks/client-sidebar-vps-rename.php:40

This is the code I've used for the hook:

<?php
use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar) {

   if (!is_null($primarySidebar->getChild('Service Details Actions'))) {
       $primarySidebar->getChild('Service Details Actions')
       ->getChild('Custom Module Button Start VM')
       ->setLabel('Power On')
       ->setIcon('far fa-play-circle');
   }
});

 

Link to comment
Share on other sites

22 minutes ago, HostingMe said:

This is the code I've used for the hook:

the golden rule to follow when modifying sidebars/navbars etc is that before you try to modify an element (e.g change its label), you need to check that the child exists - this is why your hook works when the sidebar child exists, and fails when it doesn't.

try changing your opening IF statement to below and see if that gets you any further...

if (!is_null($primarySidebar->getChild('Service Details Actions')) && !is_null($primarySidebar->getChild('Service Details Actions')->getChild('Custom Module Button Start VM'))) {
Link to comment
Share on other sites

11 minutes ago, brian! said:

the golden rule to follow when modifying sidebars/navbars etc is that before you try to modify an element (e.g change its label), you need to check that the child exists - this is why your hook works when the sidebar child exists, and fails when it doesn't.

try changing your opening IF statement to below and see if that gets you any further...


if (!is_null($primarySidebar->getChild('Service Details Actions')) && !is_null($primarySidebar->getChild('Service Details Actions')->getChild('Custom Module Button Start VM'))) {

Thank you @brian! changing the opening IF statement works perfectly. 

Link to comment
Share on other sites

Does this also work for the domain menu?

Example, set setIcon on the Overview menu or Auto Renew

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function (MenuItem $primarySidebar)
{

    if (!is_null($primarySidebar->getChild('Domain Details Actions')) && !is_null($primarySidebar
            ->getChild('Domain Details Actions')
            ->getChild('Overview')
            ->setIcon('far fa-play-circle')));

});

 

Edited by yggdrasil
Link to comment
Share on other sites

12 minutes ago, yggdrasil said:

Does this also work for the domain menu?

the principle would work on any navbar/sidebar.... though as I type this, half of your IF statement is missing, which won't be helping the situation!

12 minutes ago, yggdrasil said:

Example, set setIcon on the Overview menu or Auto Renew

yes it would work.

as a general rule, if I was modifying more than one child, then I would use getchildren and then loop though its array and make the changes to those specific children - you don't have the hassle of checking if a child exists before modifying it, because you know the child exists as it was in the array.... for one child, its hardly worth using getchildren.

Link to comment
Share on other sites

9 minutes ago, brian! said:

the principle would work on any navbar/sidebar.... though as I type this, half of your IF statement is missing, which won't be helping the situation!

yes it would work.

as a general rule, if I was modifying more than one child, then I would use getchildren and then loop though its array and make the changes to those specific children - you don't have the hassle of checking if a child exists before modifying it, because you know the child exists as it was in the array.... for one child, its hardly worth using getchildren.

Is there any link to a code example? The search in the forums seems broken. I always get the latest post as results regardless of what I search.

I also think I found a bug. While inserting an icon into a menu I noticed it adds the extra 

&nbsp; 

HTML tag that adds an extra space.

Link to comment
Share on other sites

20 hours ago, yggdrasil said:

Is there any link to a code example? The search in the forums seems broken. I always get the latest post as results regardless of what I search.

20 hours ago, yggdrasil said:

I also think I found a bug. While inserting an icon into a menu I noticed it adds the extra 

&nbsp; 

HTML tag that adds an extra space.

that's not a bug, it's intentional by design - the space is added by the sidebar.tpl template if a child has an icon.... and it's been in every version of the template since v6.0 was released in 2015 (and would occur similarly in the navbar too).

{if $item->hasIcon()}<i class="{$item->getIcon()}"></i>&nbsp;{/if}

RMe12P8.png

I guess in most sidebars where icons are used, all of the children have icons (e.g the support menu) and so you might have never noticed this occurring before - it's "fixable" in multiple ways, e.g getting the hook to add a space to the labels of the children with no icons, or adding a "blank" FA icon to those children...

<?php

# Add Icons To Service Actions Sidebar Hook
# Written by brian!

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar) {
	
	$ActionDetails = $primarySidebar->getChild("Service Details Actions");
	if (!is_null($ActionDetails)) {	
		$ActionDetailsChildren = $ActionDetails->getChildren();
		foreach($ActionDetailsChildren as $key => $Action_details_child) {
			if ($key === "Login to cPanel" || $key === "Login to Webmail"){$newicon = "fa-sign-in-alt";}
			elseif ($key === "Change Password"){$newicon = "fa-unlock";}
			elseif ($key === "Cancel"){$newicon = "fa-ban";}
			else {$newicon = "fa";}
			$ActionDetails->getChild($key)->setIcon($newicon." fa-fw");
		}
	}
});

YW5KDZ5.pnglHTUtiu.png

Link to comment
Share on other sites

3 hours ago, brian! said:

that's not a bug, it's intentional by design - the space is added by the sidebar.tpl template if a child has an icon.... and it's been in every version of the template since v6.0 was released in 2015 (and would occur similarly in the navbar too).


{if $item->hasIcon()}<i class="{$item->getIcon()}"></i>&nbsp;{/if}

RMe12P8.png

I guess in most sidebars where icons are used, all of the children have icons (e.g the support menu) and so you might have never noticed this occurring before - it's "fixable" in multiple ways, e.g getting the hook to add a space to the labels of the children with no icons, or adding a "blank" FA icon to those children...


<?php

# Add Icons To Service Actions Sidebar Hook
# Written by brian!

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar) {
	
	$ActionDetails = $primarySidebar->getChild("Service Details Actions");
	if (!is_null($ActionDetails)) {	
		$ActionDetailsChildren = $ActionDetails->getChildren();
		foreach($ActionDetailsChildren as $key => $Action_details_child) {
			if ($key === "Login to cPanel" || $key === "Login to Webmail"){$newicon = "fa-sign-in-alt";}
			elseif ($key === "Change Password"){$newicon = "fa-unlock";}
			elseif ($key === "Cancel"){$newicon = "fa-ban";}
			else {$newicon = "fa";}
			$ActionDetails->getChild($key)->setIcon($newicon." fa-fw");
		}
	}
});

YW5KDZ5.pnglHTUtiu.png

I was not aware of that extra space. I never notice it before until a few days ago while fixing some fontawesome icons. Thank you for that link. I will try experimenting a bit tomorrow and see how it goes.

Now that you posted that screenshot I'm curious about designers opinions on what is better. Icons to the left or the right of the words in terms of GUI design. Icons to the left look better visually but makes it less readable.

Happy Easter🐰  !!!

Edited by yggdrasil
Link to comment
Share on other sites

18 hours ago, yggdrasil said:

Now that you posted that screenshot I'm curious about designers opinions on what is better. Icons to the left or the right of the words in terms of GUI design. Icons to the left look better visually but makes it less readable.

my recollection is that the icons were on the left in v6, on the right in v7, back to the left in v8... no doubt we can look forward to v9 and the twenty-three theme doing something different 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