Jump to content

How to display a page's closest parent with uploaded template in find results?


Lance O.
 Share

Recommended Posts

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...