Display number of children next to the parent in the menu

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
cerpinokus

Display number of children next to the parent in the menu

Post by cerpinokus »

I'd like to display number of children next to the parent in the menu. I've tried to use the sample code from the cmsms documentation (count_numchildren), but this code only counts the number of children when the parent is the active and current page. I'd like to show the number of articles from each parent in the menu.

Should I use a database query for this or is there an easier way to accomplish this?

What I like to achieve is to get a menu on the site http://debian.textbody.com/ like this:

Root [1]
Installation [8]
Configuration [3]
etc...

So each parent displays the number of articles.
Last edited by cerpinokus on Fri Jun 22, 2007 5:43 pm, edited 1 time in total.
ars17

Re: Display number of children next to the parent in the menu

Post by ars17 »

I think you can customize the menu template using smarty to do this (much like the count children solution you mentioned). I'm relatively new to smarty so my solution might not be the most efficient or elegant, but it seems like the following will work: Inside of the menu for loop, create a variable and assign it a value that defines your current parent. Then add a nested for loop that runs through all the pages again and determines whether a page has the same current parent and count them if true with an extra if statement to keep it from counting the actual parent. Then on each display item, add an if statement that determines whether the current menu node is a parent and add the count to the menu text if true while doing nothing if false. 

I copied the the simple navigation template to a new one and customized it to do what i said above.  Like I said, I'm not the best php or smarty guy and I'm sure the other users could propose a more elegant solution, but I think this will get the job done for now.

Adam

Code: Select all

{if $count > 0}
<ul>
{foreach from=$nodelist item=node}
{if $node->depth > $node->prevdepth}
{repeat string="<ul>" times=$node->depth-$node->prevdepth}
{elseif $node->depth < $node->prevdepth}
{repeat string="</li></ul>" times=$node->prevdepth-$node->depth}
</li>
{elseif $node->index > 0}</li>
{/if}

{assign var="CurNode" value=$node->hierarchy|truncate:2:""}
{assign var="NodeCount" value=0}
{foreach from=$nodelist item=node2}
{if $node2->hierarchy|truncate:2:"" == $CurNode}
{if $node2->hierarchy == $CurNode}
{else}
{assign var="NodeCount" value=$NodeCount+1}
{/if}
{/if}
{/foreach}


{if $node->current == true}
  {if $node->hierarchy == $CurNode}
    <li class="currentpage"><h3><dfn>Current page is {$node->hierarchy}: </dfn>{$node->menutext}&nbsp[{$NodeCount}]</h3>
  {else}
    <li class="currentpage"><h3><dfn>Current page is {$node->hierarchy}: </dfn>{$node->menutext}</h3>
  {/if}

{elseif $node->parent == true}
<li class="activeparent"><a class="activeparent" href="{$node->url}"{if $node->accesskey != ''} accesskey="{$node->accesskey}"{/if}{if $node->tabindex != ''} tabindex="{$node->tabindex}"{/if}{if $node->titleattribute != ''} title="{$node->titleattribute}"{/if}><dfn>{$node->hierarchy}: </dfn>{$node->menutext}&nbsp[{$NodeCount}]</a>

{elseif $node->type == 'sectionheader'}
  {if $node->hierarchy == $CurNode}
    <li class="sectionheader">{$node->menutext}&nbsp[{$NodeCount}]
  {else}
    <li class="sectionheader">{$node->menutext}
  {/if}
  
{elseif $node->type == 'separator'}
<li class="separator" style="list-style-type: none;"> <hr />

{else}
  {if $node->hierarchy == $CurNode}
    <li><a href="{$node->url}"{if $node->accesskey != ''} accesskey="{$node->accesskey}"{/if}{if $node->tabindex != ''} tabindex="{$node->tabindex}"{/if}{if $node->titleattribute != ''} title="{$node->titleattribute}"{/if}{if $node->target != ''} target="{$node->target}"{/if}><dfn>{$node->hierarchy}: </dfn>{$node->menutext}&nbsp[{$NodeCount}]</a>
  {else}
    <li><a href="{$node->url}"{if $node->accesskey != ''} accesskey="{$node->accesskey}"{/if}{if $node->tabindex != ''} tabindex="{$node->tabindex}"{/if}{if $node->titleattribute != ''} title="{$node->titleattribute}"{/if}{if $node->target != ''} target="{$node->target}"{/if}><dfn>{$node->hierarchy}: </dfn>{$node->menutext}</a>
  {/if}
{/if}

{/foreach}
{repeat string="</li></ul>" times=$node->depth-1}</li>
</ul>
{/if}
cerpinokus

Re: Display number of children next to the parent in the menu

Post by cerpinokus »

Thank you for the code. It still only outputs the correct number of children if the parent is the current page. If not, it shows 0 as the number of children. The smarty code seems to ignore children completely if the parent is not the active page.

It's a pity, I also thought there would be a way to solve this with the template code.
cerpinokus

Re: Display number of children next to the parent in the menu

Post by cerpinokus »

That code doesn't solve it. I'd like to show the number of children for EACH parent in the menu and not just the active one. The active parent expands the children, so it's more interesting to know how many children the other parents have, since they don't expand.
ars17

Re: Display number of children next to the parent in the menu

Post by ars17 »

OK, i see the problem now...I tested the code in a test page and it worked for me, but i didn't have a collapse='1' in my menu tag. So, what I posted will work if you don't collapse...sorry about that.  I guess this makes sense since the template only handles children when its parent is selected if collapse is true.  In order to do what you want to do, you probably need to add some code to the menu manager php to count the children and then pass that count to the template in a variable.
ars17

Re: Display number of children next to the parent in the menu

Post by ars17 »

Alright, lets try this again...

in MenuManager.module.php, there is a function called "FillNode" which fills the variables passed to the template.  I think if you add the following line:

Code: Select all

$onenode->childcnt = count($node->getChildren());
you can pass the children count to the menu template in the variable childcnt.  I tested this on my test site (with collapse enabled this time) and it seems to work.

Adam
cerpinokus

Re: Display number of children next to the parent in the menu

Post by cerpinokus »

Ty :) I'll try it out and let you know  ;)
cerpinokus

Re: Display number of children next to the parent in the menu

Post by cerpinokus »

No luck so far  :( Where ever I place the code it doesn't return a value. I do see a variable $children in the section above FillNode, but that doesn't work either.

If you say it works for you I am curious to see where you placed the code.
ars17

Re: Display number of children next to the parent in the menu

Post by ars17 »

If you look at the menu templates, you'll see variables in the form: "$node->menutext" which seemed to be defined in the FillNode function. By adding that line, we're just piggybacking on that and adding an additional variable. So where ever you want the children count to be, you need to add "{$node->childcnt}" to the template.  For example:

Code: Select all

<li class="currentpage"><h3><dfn>Current page is {$node->hierarchy}: </dfn>{$node->menutext}&nbsp[{$node->childcnt}]</h3>
which should output something like "Home [3]" depending on the menu text and the count.  That's how it worked for me.

Adam
wuschel

Re: Display number of children next to the parent in the menu

Post by wuschel »

function called "FillNode"
method derefrencing is not supported by smarty
cerpinokus

Re: Display number of children next to the parent in the menu

Post by cerpinokus »

I got the code right, just misplaced it a bit :D

Thx for your help, this works well :)

The tweaked version prevents zero's from being shown and adds a ">>" sign:

Code: Select all

	if (count($node->getChildren()) > 0)
	{$onenode->childcnt = count($node->getChildren()).'»';}
Last edited by cerpinokus on Mon Jun 25, 2007 10:13 pm, edited 1 time in total.
Locked

Return to “CMSMS Core”