Lance O. Posted March 3, 2014 Share Posted March 3, 2014 A site I am currently developing has a need for multiple levels of container pages, named as "Category" and "Subcategory" below. Pages that include files can be added as children to both the "Category" and "Subcategory" container pages. Pages named "Page" and "Subpage" display the content of child "Category", "Subcategory", and "File" pages. The "Category", "Subcategory", and "File" pages do not use a an uploaded template, so viewing those pages in a browser will produce a 404 error. - Home -- Page 1 --- Category 1 ---- File 1 ---- File 2 ---- Subcategory 1 ---- File 3 ---- File 4 -- Page 2 --- Subpage 1 ---- Category 2 ----- File 5 ----- File 6 ----- Subcategory 2 ------ File 7 ------ File 8 My question is, when a user performs a find on the site and finds one of the "Category", "Subcategory", or "File" pages, how I do I display the found page's closest parent that uses an uploaded template? For example, in the file structure above, how can I display the "Page 1" page when a user searches for "File 1" or "File 3"? How can I display "Subpage 1" when a user searches for "File 5" or "File 7"? Link to comment Share on other sites More sharing options...
horst Posted March 3, 2014 Share Posted March 3, 2014 You can traverse the "parent" property of the found page of type category | subcategory | file until you find a page that has a template file. Here is some pseudo code: while($page->template->filename == "") { $page = $page->parent; } I haven't tested this and I'm not sure if this is working. It is meant as starting point. Link to comment Share on other sites More sharing options...
Soma Posted March 3, 2014 Share Posted March 3, 2014 If you have Page and Subpage's display the content, you already have it. Link to comment Share on other sites More sharing options...
Soma Posted March 3, 2014 Share Posted March 3, 2014 Edit: Ah or something like this? $page->parent("template=basic-page")->url; Will return the first parent found with the template "basic-page" going from from $page Link to comment Share on other sites More sharing options...
adrian Posted March 4, 2014 Share Posted March 4, 2014 Well it sounds like you are only after those templates with a template file. Actually can't think of how to do this with a selector right now, but you could find all parents of the page and then foreach through these and do a: foreach($page->parents as $parent){ if($parent->viewable()) { echo $parent->title; break; } } https://github.com/ryancramerdesign/ProcessWire/blob/03387f8283d518e9cc405eff8f05cd6a5bf77c4c/wire/modules/PagePermissions.module#L166 The will not only check to see if there is a template php file, but also check to make sure the user has view permission for the template. I haven't tested this, so I am not sure if the echo page title will be the closest parent or not, but hopefully it might get you going in the right direction? Link to comment Share on other sites More sharing options...
Lance O. Posted March 4, 2014 Author Share Posted March 4, 2014 Is there a way to find a page's closest parent that has an uploaded template file if I don't know what that template file name is? Link to comment Share on other sites More sharing options...
Soma Posted March 4, 2014 Share Posted March 4, 2014 Adrian already gave answer. Apart from you should know what templates have a file/view. foreach($resultpage->parents()->reverse() as $parent) { if($parent->viewable()) { // we got a viewable parent } } Link to comment Share on other sites More sharing options...
Lance O. Posted March 4, 2014 Author Share Posted March 4, 2014 Thank you Soma, adrian, and horst! This seems to work: foreach ( $results as $item ) { if ( $item->matches("template=resource-category|resource-subcategory|resource-file|resource-link") ) { foreach ( $item->parents() as $parent ) { if ( $parent->template->filenameExists() ) { $url = $parent->url; $title = $parent->title; } } } else { $url = $item->url; $title = $item->title; } echo "<a href='{$url}'>{$title}</a>"; } 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