thetuningspoon Posted December 23, 2016 Share Posted December 23, 2016 I am trying to do something similar to a $page->rootParent check, but for a site using Soma's multi-site module. Because the "roots" for a multi-site are actually the 2nd level pages rather than the first level, this means that $page->rootParent doesn't give me what I need to check which "section" of the site I'm currently in. This made me wonder, shouldn't there be a simple $page->level or $page->depth property that returns how deep any page is located in the page tree? Then we could do things like $page->parents('level=2') to get a parent at a specific level, $page->level to determine whether to show/hide some element in a template, or $pages->find('level=2') to get all pages at a certain level. I built some hooks to add in this functionality, although it won't work for database selectors: /** * level * * @return int - Level/depth of this page in the page tree */ $this->wire()->addHookProperty('Page::level', function($e) { $e->return = $e->object->parents->count(); }); /** * parentLevel(int $level = null) * * @param int $level * @return - Returns the Page's parent page at the requested depth/level in the page tree. Returns NullPage if there is no page at that level */ $this->wire()->addHook('Page::parentLevel', function($e) { $that = $e->object; $level = $e->arguments[0] ?: null; if($level == null) $e->return = new NullPage(); else { $parents = $that->parents->append($that); if($parents->count() < $level) { $e->return = new NullPage(); } else { $e->return = $parents->eq($level); } } }); 3 Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 23, 2016 Share Posted December 23, 2016 This is certainly a nice idea. I'd also really like to see this as database level selector. And you can shorten the method hook quite a bit like this: $this->wire()->addHook('Page::parentLevel', function(HookEvent $event) { $level = (int) $event->arguments(0); if($level < 0) return $event->return = new NullPage(); $event->return = $event->object->parents->append($event->object)->eq($level) ?: new NullPage(); }); Edit: Without the second line in the hook it would also accept $page->parentLevel(-3), which goes from the current page upwards. 3 Link to comment Share on other sites More sharing options...
thetuningspoon Posted December 23, 2016 Author Share Posted December 23, 2016 Thanks for the more compact version, LostKobrakai. I didn't get enough sleep last night so my code was probably a bit sloppy. I still haven't thoroughly tested it either. Link to comment Share on other sites More sharing options...
teppo Posted December 23, 2016 Share Posted December 23, 2016 I wouldn't mind having a $page->depth property (in my opinion depth is more descriptive than level, but that might be just because I'm used to it from other systems) available out of the box either, though I do also wonder how useful it would actually be? Right now I can't think of any past situation where I would've needed this for selectors, and on the front-end counting the number of parents is so simple that it hardly makes sense to have a dedicated feature for it. In my opinion it only deserves to become a built-in property/feature if it's indeed a (very) commonly needed thing. If not, it should be implemented on a case-by-case basis as a hook or module or whatever Link to comment Share on other sites More sharing options...
thetuningspoon Posted December 23, 2016 Author Share Posted December 23, 2016 I guess I always found it strange that there was a rootParent property, since the determination of the rootParent as the 2nd level parent page seems a bit arbitrary and opinionated compared to most of ProcessWire's very un-opinionated API. I think the level could come in handy in many cases, though admittedly I can't name them off the top of my head! I know that I have had several use cases for this in the past where I've had to do the $page->parents->count() trick. I think being able to search for a page something like $pages->find('template=folder, level=2') could also come in handy, though I assume this can also be achieved now with $pages->find('template=folder, parents.count=2') now (though I haven't tested that). It would just be a lot clearer for non or newbie-programmers to realize the capability is there. Edit: One common place where the level property could come in handy is building menus as well as determining whether to show a submenu on a page or not. Generally this should be based on your depth in the page tree rather than on the template or a field setting. Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted October 13, 2021 Share Posted October 13, 2021 Solution:https://processwire.com/api/ref/page/num-parents/ 2 Link to comment Share on other sites More sharing options...
thetuningspoon Posted October 13, 2021 Author Share Posted October 13, 2021 Does numParents work in a selector? Link to comment Share on other sites More sharing options...
Robin S Posted February 16 Share Posted February 16 A module that adds support for finding and sorting pages by depth in a PageFinder selector: 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now