HostingMe Posted April 2, 2021 Share Posted April 2, 2021 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'); } }); 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted April 2, 2021 Share Posted April 2, 2021 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'))) { 0 Quote Link to comment Share on other sites More sharing options...
HostingMe Posted April 2, 2021 Author Share Posted April 2, 2021 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. 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted April 3, 2021 Share Posted April 3, 2021 (edited) 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 April 3, 2021 by yggdrasil 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted April 3, 2021 Share Posted April 3, 2021 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. 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted April 3, 2021 Share Posted April 3, 2021 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 HTML tag that adds an extra space. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted April 4, 2021 Share Posted April 4, 2021 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 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> {/if} 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"); } } }); 0 Quote Link to comment Share on other sites More sharing options...
yggdrasil Posted April 4, 2021 Share Posted April 4, 2021 (edited) 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> {/if} 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"); } } }); 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 April 4, 2021 by yggdrasil 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted April 5, 2021 Share Posted April 5, 2021 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. 🙄 0 Quote Link to comment Share on other sites More sharing options...
bear Posted April 5, 2021 Share Posted April 5, 2021 13 minutes ago, brian! said: no doubt we can look forward to v9 and the twenty-three theme doing something different again I haven't yet seen them in the middle of the phrase? 😛 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted April 6, 2021 Share Posted April 6, 2021 20 hours ago, bear said: I haven't yet seen them in the middle of the phrase? 😛 your sense of sarcasm is worrying close to mine (or vice-cersa!) because I had the same thought... I was tempted to mock it up, but couldn't be bothered! 0 Quote Link to comment Share on other sites More sharing options...
bear Posted April 6, 2021 Share Posted April 6, 2021 Perhaps this one? 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.