Blueprint query grandChildrenAndDrafts

I’ve been using querys to make a list of all children of a page including all Drafts like this:

query: page.childrenAndDrafts

But now I need to do the same with with all grandChildren and Drafts i’ve tried these:

query: page.grandChildrenAndDrafts

Returns this error:
Invalid query result - Result must be of type Kirby\Cms\Pages, Kirby\Cms\Field given.

query: page.childrenAndDrafts.childrenAndDrafts

Returns this error:
Invalid query result - Result must be of type Kirby\Cms\Pages, NULL given.

query: page.children.childrenAndDrafts

Returns this error:
Invalid query result - Result must be of type Kirby\Cms\Pages, NULL given.

I dont really get why these error get thrown. To me this makes sense since I want the children of the current page and then get all children of that page including all drafts.
Is there another way to do this or is my syntax just wrong?

All the methods you are trying do not exists.

While a single page has a grandChildrenAndDrafts() methods, a pages collection doesn’t.

You could either create a custom collection or create a custom pages method.

1 Like

Would it also work to override the Pages Class from the Kirby core?
I was looking into this https://getkirby.com/docs/guide/templates/page-models#overriding-the-page-class

Since when I place my function into the kirby core src folder Page Class (kirby/src/cms/page.php) the method can then be used in the blueprint. This is the function when I place it there:

public function grandChildrenAndDrafts() {
    return $this->children()->children()->drafts();
}

But when creating a new class to extend the core Page class like this:

class GrandChildrenModel extends Page {
    public function grandChildrenAndDrafts() {
        return parent::children()->children()->drafts();
    }
}

For some reason I cant use it in my blueprint and I still get the following error:

Invalid query result - Result must be of type Kirby\Cms\Pages, Kirby\Cms\Field given.

I have created a file inside /site/models/ named “endkunden.php” since the page in the backend that needs to use this function is called endkunden.txt and uses the template “endkunden”

Your function should look like this:

public function grandChildrenAndDrafts() {
  return $this->children()->drafts()->merge($this->grandChildren());
}

Same error. It seems no matter what I write into this function it just wont work even when just returning $page or $pages

This code even works when adding it to the core kirby page class:

public function grandChildrenAndDrafts() {
    return $this->children()->drafts();
}

To me it seems like the Model just doesn’t get registered. Even though the file is named exactly like the page template and it is in the correct folder.
No matter what i try to return in my method I wont get a result in my backend.

Found the Problem. The Class has to be named like this in order to work: “{templatename}Page”

This was my old class:

class Endkunden extends Page {
    public function grandChildrenAndDrafts() {
        return $this->children()->drafts()->merge($this->grandChildren());
    }
}

And this is the working Version:

class EndkundenPage extends Page {
    public function grandChildrenAndDrafts() {
        return $this->children()->drafts()->merge($this->grandChildren());
    }
}