isSiblingSelected?
13 posts by 3 authors in: Forums > CMS Builder
Last Post: July 20, 2011 (RSS)
By Perchpole - June 27, 2011
I'm trying to develop a localised menu to display links to related categories. I've tried using various built-in getCategories() code snippets as filters, such as _isAncestorSelected and _isSelectedBranch, etc., but the results aren't always as expected (when dealing with multi-tier category trees).
What I'd really like is something which would work along the lines of _isSiblingSelected.
This code isn't yet built into CMSB - so is there any way I can mimic it?
:o/
Perch
Re: [Perchpole] isSiblingSelected?
By Dave - June 28, 2011
I can add that to the next version and give you some code for it. How would we define _isSiblingSelected? All records at the same depth with the same parent as the selected category?
interactivetools.com
Re: [Dave] isSiblingSelected?
By Dave - June 28, 2011
interactivetools.com
Re: [Dave] isSiblingSelected?
By Perchpole - June 28, 2011
That sounds perfect and would make an excellent filter.
So would _isParentSelected
It's a bit of a more specific than _isAncestorSelected.
Can we add that to you list too, please?!
:0)
Perch
Re: [Perchpole] isSiblingSelected?
By Dave - June 28, 2011
Try this:
- Open lib/viewer_functions.php
- Search for _hasSiblings
- Add the code in red:
$category['_hasSiblings'] = intval( !($category['_isFirstChild'] && $category['_isLastChild']) );
// added in 2.10
$category['_isSiblingSelected'] = intval( $selectedCategory && $selectedCategory['parentNum'] == $category['parentNum'] && !$category['_isSelected'] );
$category['_isParentSelected'] = intval( $selectedCategory && $selectedCategory['num'] == $category['parentNum'] );
$category['_listItemStart'] = _getListItemStartTags($prevCategory, $category, $nextCategory, $options);
Hope that helps, let me know if that works for you!
interactivetools.com
Re: [Dave] isSiblingSelected?
By Perchpole - June 29, 2011
Brilliant. Thanks for this. It works really well.
Can we complete the deck and have _isChildSelected?
Again, it would be a bit more specific than _isDecendantSelected.
:0)
Perch
Re: [Perchpole] isSiblingSelected?
By Dave - June 29, 2011
Sure, try this:
$category['_isChildSelected'] = intval( $selectedCategory && $category['num'] == $selectedCategory['parentNum'] );
Let me know if you need any other changes, otherwise I'll include these all in 2.10.
interactivetools.com
Re: [Dave] isSiblingSelected?
By Perchpole - June 29, 2011
This is great. Better than great! Thanks.
:0)
Perch
Re: [Perchpole] isSiblingSelected?
By Mikey - June 29, 2011
Dave -
This is great. Better than great! Thanks.
:0)
Perch
Perch, can you provide some code showing how this came together... I thick this is just what I've been searching for.
Zick
Re: [zick] isSiblingSelected?
By Perchpole - June 30, 2011 - edited: June 30, 2011
I'm currently building a site for a large and long-established charity. As such, their "About Us" section contains upwards of 30 different pages across various sub-sections. I wanted to introduce a localised sub-section menu which would enable readers to move around the related pages.
The category tree looks something like this:
[pre]Home
Articles
News
About Us
Our History
Page2
Page3
Page4
Our Founder
Page2
Page3
Page4
Etc...
[/pre]
So, in our example, if a reader was on the "Our History" page, I wanted to display a small menu showing just those links related to that sub-section:
[pre]Our History
Page2
Page3
Page4[/pre]
Achieving this (with simple code) using the current filters was proving difficult. The _isAncestorSelected or _isDecendantSelected snippets returned too many links - often the entire branch of the ancestory tree.
With Dave's updates all I need is this:
<?php
$subMenuLinks = "";
foreach ($categoryRecords as $menuCategory) {
if ($menuCategory['_isSelected'] || $menuCategory['_isParentSelected'] || $menuCategory['_isChildSelected']) {
$subMenuLinks[] = $menuCategory;
}
}
?>
This pulls out only those links which are directly related to each category in the sub-section.
Works a treat!
:0)
Perch