Apply field value from parent to children inside a category section

9 posts by 3 authors in: Forums > CMS Builder
Last Post: June 8, 2010   (RSS)

By aev - April 21, 2010

Hi,

We have a field called 'hideNav' which is set to the value '1' for some records inside a category section. We would like all records descending from such a record to get the same 'hideNav' value. I.e. apply the value from the record to all it's children.

Any ideas?

PS: For this project we can limit this check to depth0 records..

-aev-

Re: [aev] Apply field value from parent to children inside a category section

By Jason - April 21, 2010

Hi,

If you only use 'hideNav' when displaying, for a child you could check to see if the parent has 'hideNav' set.

Could you maybe give an example of what you're trying to do?

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Apply field value from parent to children inside a category section

By aev - April 21, 2010

If you only use 'hideNav' when displaying, for a child you could check to see if the parent has 'hideNav' set.

Yes that would be another way of doing it, but I would prefer the other way around. But I'm open for all solutions.


Could you maybe give an example of what you're trying to do?

We have a top menu (nav1) created from all the depth0 records in a category section, and a sidebar menu (nav2) created from the children of the selected depth0 record. We want to control if to generate the nav2 menu from a setting in each depth0 records. So far everything is simple, but we also need all the children to know that they should hide nav2 automatically from the setting in the parent dept0 record.


-aev-

Re: [aev] Apply field value from parent to children inside a category section

By Dave - April 21, 2010

Hi aev,

This one is pretty advanced and may take some thinking and/or custom code... Some ideas:

- In the admin: You could write a plugin so when you set/unset hidenav of a category record all the children got the same setting.

- When displaying: When displaying each record you could loop over all the parents and see if any of them had hidenav set. Here's some example untested pseudo-code:

<?php
$parentHideNav = false;
$parentNums = explode(":", $category['lineage'];
foreach ($parentNums as $parentNum) {
if (@$categoriesByNum[$parentNum]['hidenav']) { $parentHideNav = true; }
}
?>


Or maybe to that at the top and set a fake pseudo-field called _parentHidden for each child.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Apply field value from parent to children inside a category section

By aev - April 22, 2010

Hi Dave,

I really think the 'display' code you wrote will do the trick! Thanks!

Regarding writing a plugin, that would be very interesting. Do you provide any documentation on this? A beginners tutorial?

-aev-

Re: [aev] Apply field value from parent to children inside a category section

By Dave - April 22, 2010

Hi Aev,

We don't have any docs on writing a plugin yet, and it requires a lot of knowledge (or "Find in Files..." searches) of CMSB since it's modifying how it works.

But have a look at the example plugins attached to this thread and feel free to post any questions.
http://www.interactivetools.com/forum/gforum.cgi?post=77765#77765

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [aev] Apply field value from parent to children inside a category section

By Dave - June 8, 2010

Hi aev,

Looks like we're not defining that anywhere. Assuming your variable is $categoryRecords try this:

$categoriesByNum = array_combine(array_pluck($categoryRecords , 'num'), $categoryRecords);

Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Apply field value from parent to children inside a category section

By aev - June 8, 2010


Now it works!

Great feature, will use this for other things as well.


Thanks!