Jump to content

Please a little help with hooks and renaming sidebar items


ARKO

Recommended Posts

Hi,

I'm using multiple templates, and I want to change files only if it's really needed. As a example I want to change names of items in the sidebar, to better match my needs. In the TPL files there is a code like this:

        {if $item->hasChildren()}
            <div class="list-group{if $item->getChildrenAttribute('class')} {$item->getChildrenAttribute('class')}{/if}">
                {foreach $item->getChildren() as $childItem}
                    {if $childItem->getUri()}
                        <a menuItemName="{$childItem->getName()}" href="{$childItem->getUri()}" class="list-group-item{if $childItem->isDisabled()} disabled{/if}{if $childItem->getClass()} {$childItem->getClass()}{/if}{if $childItem->isCurrent()} active{/if}"{if $childItem->getAttribute('dataToggleTab')} data-toggle="tab"{/if}{if $childItem->getAttribute('target')} target="{$childItem->getAttribute('target')}"{/if} id="{$childItem->getId()}">
                            {if $childItem->hasBadge()}<span class="badge">{$childItem->getBadge()}</span>{/if}
                            {if $childItem->hasIcon()}<i class="{$childItem->getIcon()}"></i>&nbsp;{/if}
                            {$childItem->getLabel()}
                        </a>
                    {else}
                        <div menuItemName="{$childItem->getName()}" class="list-group-item{if $childItem->getClass()} {$childItem->getClass()}{/if}" id="{$childItem->getId()}">
                            {if $childItem->hasBadge()}<span class="badge">{$childItem->getBadge()}</span>{/if}
                            {if $childItem->hasIcon()}<i class="{$childItem->getIcon()}"></i>&nbsp;{/if}
                            {$childItem->getLabel()}
                        </div>
                    {/if}
                {/foreach}
            </div>
        {/if}

With some information on this community, I find out how to rename the sidebar items by a hook and ended using this code:

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 1, function(MenuItem $primarySidebar)
{
   if (!is_null($primarySidebar->getChild("My Info"))){
       if (!is_null($primarySidebar->getChild("My Info")->getChild("My Requests"))){
               $primarySidebar->getChild("My Info")
               ->getChild("My Requests")
               ->setLabel("My Topics");
       }
   }
   if (!is_null($primarySidebar->getChild("Group Actions"))){
       if (!is_null($primarySidebar->getChild("Group Actions")->getChild("Add New Request"))){
               $primarySidebar->getChild("Group Actions")
               ->getChild("Add New Request")
               ->setLabel("Add New Topic");
       }
   }
});



But this is only working on the sidebar items, that has been checked in the first command as "{if $childItem->getUri()}". I tried a lot of things, but how can I replace the names of the second part? They are not with a uri link, only have a badge and count. When I set the renaming in the .tpl file, it's working but when I use a hook I can't get it working. So I hope someone can tell me, what I didn't check good enough 😃

Kind regars,
Arjen Kocken

Edited by ARKO
Link to comment
Share on other sites

Arjen,

On 04/06/2021 at 07:42, ARKO said:

I tried a lot of things, but how can I replace the names of the second part?

if these are default sidebars, e.g they are in WHMCS out of the box, then you should be able to change their labels using Language Overrides without the need for a hook... if these labels only change for specific templates, then yes you might need a hook for that.

$_LANG['yourinfo'] = "Your Information";

9AEBjzg.pnghEwDXX1.png

remember to create a relevant language override for every language you need to change these labels for, e.g if your site only uses English and Dutch, then you only need to create overrides for those two languages.

if these are custom sidebars, e.g you're creating them via a hook, then I would set the label during their creation rather than have one hook to create them and then a second hook to change their labels.

Link to comment
Share on other sites

Hi Brian,

Thanks for your reply, and I already did all overrides by language, the only things that are not set by the language files and template files are the sidebar items. I'm afraid they are part of the encrypted module files.

One I get fixed with the hook above, and the second one I get fixed now with adding this to: "template\includes\sidebar.tpl"

                        <div menuItemName="{$childItem->getName()}" class="list-group-item{if $childItem->getClass()} {$childItem->getClass()}{/if}" id="{$childItem->getId()}">
                            {if $childItem->hasBadge()}<span class="badge">{$childItem->getBadge()}</span>{/if}
                            {if $childItem->hasIcon()}<i class="{$childItem->getIcon()}"></i>&nbsp;{/if}

                            {* Addon: Feature Requester *}
                            {if $smarty.server.REQUEST_URI|strstr:'m=featurerequester'}
                                {* Rename text only menu items, without URI *}
                                {if $childItem->getName() eq 'Total Requests'}Total Topics{else}
                                {* Run default command, to get labels for without URI *}
                                {$childItem->getLabel()}
                                {* Close all the {else} statement, with one {/if} per {else} *}
                            {/if}
                            {* Stop: Feature Requester *} 
                            
                            {else} 
                            {$childItem->getLabel()}
                            {/if}

                        </div>

And I get the page header fixed by adding this code to:  "template\includes\pageheader.tpl"

{* Start: Pageheader customs *}
{* This file must be included on top of every template: "$template/includes/pageheader.tpl" *}
{include file="_q/templates/pageheader-customs.tpl" scope='global'}
{* End: Pageheader customs *}

<div class="header-lined">
    <h1>{$title}{if $desc} <small>{$desc}</small>{/if}</h1>
    {if $showbreadcrumb}{include file="$template/includes/breadcrumb.tpl"}{/if}
</div>

Where the file "_q/templates/pageheader-customs.tpl" is having this code:

{* Start: Pageheader customs *}
{* This file must be included on top of every template: "$template/includes/pageheader.tpl" *}
{* {include file="_q/templates/pageheader-customs.tpl" scope='global'} *}
{* End: Pageheader customs *}

{capture assign='pageheader-customs'}
{* Addon: Feature Requester *}
{* Titles *}
    {* Index *}
    {if $smarty.server.REQUEST_URI|strstr:'m=featurerequester'}
        {if $title eq 'Feature Requests'} {$title = 'Features and requests'}{/if}
    {/if}
{* Stop: Addon*}
{/capture}

What I really like to know, is:

Is it possible to replace this TPL modifications with hooks, because I'm building a WHMCS environment with multiple domains and different WHMCS templates, and I want to try to avoid to touch TPL files or what ever files that needs to be modified as much as possible. Let's say I have a lot of modules and a lot of templates, It was already a big learning curve to figure out how I can make it as flexible as possible, to manage all this without getting crazy of al possible * that can be created. So I'm loving Github more than anything in the world, to get this job done so far.

Si I was hoping, that this TPL modifications can be done by hooks, because when it's done by hooks it's added to all templates. Do you have any idea if it's possible?

Kind regards 

Edited by ARKO
Link to comment
Share on other sites

On 09/06/2021 at 15:18, ARKO said:

I'm afraid they are part of the encrypted module files.

does it have any language files of its own in the relevant addon module folder, or do you think the strings are hardcoded within the code itself ?

I assume the addon module is SwiftModders Feature Requester ? if so, it's not changing the strings on a language change, so there can't be multiple language files within the addon.

On 09/06/2021 at 15:18, ARKO said:

One I get fixed with the hook above, and the second one I get fixed now with adding this to: "template\includes\sidebar.tpl"

so it's this sidebar... ?

4q4WzLO.png

the trick with Sidebars is to not necessarily worry about the URI (though you can use that if you have to) - focus on the menuitemname values and then use them in your ->getChild values in the hook.

u9V6vN8.png

so as per your hook, if you wanted to change the My Requests to My Topics, it should be...

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimarySidebar', 10, function(MenuItem $primarySidebar) {
  
	if (!is_null($primarySidebar->getChild('My Info')) && !is_null($primarySidebar->getChild('My Info')->getChild('My Requests'))) {  
		$primarySidebar->getChild('My Info')->getChild('My Requests')->setLabel('My Topics');
	}
});

if it doesn't work, then I suspect the above hook might be running *before* the addon module has used its own hook to create the custom sidebar in the first place - hence the hook has nothing to modify.... changing the priority value on your hook might help (10 above), but you might need to double check with SwiftModders what the priority of their hook is and adjust accordingly.

On 09/06/2021 at 15:18, ARKO said:

So I was hoping, that this TPL modifications can be done by hooks, because when it's done by hooks it's added to all templates. Do you have any idea if it's possible?

in terms of the second one, that looks just like a variation of a breadcrumb hook to me...

<?php

add_hook('ClientAreaPage', 1, function($vars) {	

	$breadcrumb = $vars['breadcrumb'];
	if ($breadcrumb[1]['label'] == "Feature Requests") {
		$breadcrumb[1]['label'] = "Features and Requests";
		return array('breadcrumb' => $breadcrumb);
	}
});

goor6di.png

that "Features & Requests" text could be a language string if you ever needed it to be multilingual, e.g by creating the overrides and using Lang::trans instead.... you can search Lang::trans and my username and you should find loads of working hook examples that uses it.

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