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;
}
};