Passing field value/variable to child pages

I’m currently building some docs pages with a treemenu structure. The treemenu is built based on what the top level is, and that’s currently set by getting the first parent in the directory structure.

The problem is that this could be used in other folders that aren’t necessarily top level, and I don’t necessarily know their name. So what I need to be able to do is say “this page is the parent that the treemenu should start at, and make sure all children of this page know to start at this page when building the menu.”

I’m trying to avoid a couple of things:

  1. Hardcoding this into the templates. This would require new templates for each place this is used.
  2. Having a field that I have to enter on all subpages of the structure.

I would be fine with a field on the parent page, as well as a different template for the parent page from all the subpages if that’s necessary. Maybe there’s a way to walk up the parent tree in the template looking for that field, and then set it based on the first parent that has it?

Guess you can traverse the tree up using $page->parents() and check if any of these parent pages contains the field and return that page. Could be streamlined with a custom page method, something like:

<?php

page::$methods['getTreemenuRoot'] = function($page, $field) {
  if($page->parents()->count()) {

    foreach($page->parents() as $p) {
      if ($p->$field()->bool()) {
        return $p;
        exit();
      } else {
        return $page;
      }
    }
  } else {
    return $page;
  }
};

Usage:

$treeMenuRoot = $page->getTreemenuRoot('somefield');

You can of course hardcode the field in the page method if you don’t want to pass it as parameter.

Thanks. That’s kind of the direction I was thinking. The page method is a nice extra touch.

Hmm…it’s not behaving entirely as expected. I’ll have to dig into it a bit more when I get the chance and figure out why.

Let me know where it fails, I can well imagine that there are some pitfalls.

It seems to work fine on first level pages. I guess second level, below docs. Then it fails when it hits the third level. So maybe it’s not actually traveling all the way up the tree, or else it’s not looping through all the parents properly, but just getting the first parent. Does that make sense?

I have a lot of other stuff to do on this to get it ready for a demo, so I haven’t had time to troubleshoot it properly.

This should work now (at least it looks like it)

<?php
page::$methods['getTreemenuRoot'] = function($page, $field) {

  // check if the page has parents
  if($page->parents()->count()) {
    // loop through parents
    foreach($page->parents() as $parent) {

      if(!$parent->$field()->bool()) continue;
      if ($parent->$field()->bool()) { return $parent ; exit;}

    }
    return $page;

    // if the page doesn't have parents, return the page itself
  } else {
    return $page;
  }
};
1 Like

Just now got around to testing this. Works perfectly, thanks!