Children cant be found

Hi,
a friend of mine needs help with his kirby site, which was built by an external agency. The code makes sense so far but I don’t understand one thing and I think you can help me with that.

The page has events and they are shown on a eventpage obviously. So far all events are on one page. The customer wants to split the events now into online- and offline-events. The events are subpages and the blueprints etc. are set up.
There is a site controller which says
$eventsPage = $pages->filterBy('template', 'events')->first();
so I just copied it and set up
$offlineEventsPage = $pages->filterBy('template', 'offline-events')->first();

I have copied the template code, which is now like this:

<?php
$offlineEvents = [];
$eventsOnDemand = [];

foreach ($offlineEventsPage->children() as $child) {
    $dates = [];

    foreach ($child->content()->dates()->toStructure() as $item) {
        $dates[] = [
            "startDate" => $item->datestart()->toDate("d. F Y"),
            "endDate" => $item->dateend() ? $item->dateend()->toDate("d. F Y") : null
        ];
    }

    usort($dates, function ($a, $b) {
        return strtotime($a["startDate"]) - strtotime($b["startDate"]);
    });

    $dates = array_filter($dates, function ($date) {
        return strtotime($date["startDate"]) >= strtotime("today");
    });

    if (count($dates) === 0 && !$child->content()->datesondemand()->toBool()) {
        continue;
    }

    $event = [
        "category" => $child->content()->category()->toString(),
        "startDate" => count($dates) > 0
            ? $dates[key($dates)]["startDate"]
            : "",
        "endDate" => count($dates) > 0
        && $dates[key($dates)]["endDate"]
        && $dates[key($dates)]["endDate"] !== $dates[key($dates)]["startDate"]
            ? " bis " . $dates[key($dates)]["endDate"] . " "
            : " ",
        "headline" => $child->content()->headline()->toString(),
        "link" => $child->url(),
        "showMore" => count($dates) > 1 ? "und weitere Termine" : "",
        "onDemand" => count($dates) === 0 && $child->content()->datesondemand()->toBool()
            ? "Auf Anfrage"
            : ""
    ];

    if (count($dates) === 0 && $child->content()->datesondemand()->toBool()) {
        $eventsOnDemand[] = $event;
    } else {
        $offlineEvents[] = $event;
    }
}

usort($offlineEvents, function ($a, $b) {
    $t1 = strtotime($a["startDate"]);
    $t2 = strtotime($b["startDate"]);
    return $t1 - $t2;
});

return array_merge($offlineEvents, $eventsOnDemand);

The page has events as sub pages like the online-events has. The online events are showing up but on the page “offline” it says:

Call to a member function children() on null

for the line foreach ($offlineEventsPage->children() as $child) {

Why can’t the offline events (children) be found?

That’s not the problem. As the error message says, you are calling the children() method on null. Which means that the variable $offlineEventsPage is null and not a Page object, which in turn means that there is some issue with

$offlineEventsPage = $pages->filterBy('template', 'offline-events')->first();

So there doesn’t seem to be a page within the top level pages which uses the template offline-events or the template doesn’t exist.

1 Like

Thats true, there is no top level page with that template, there is only a subpage with it. So actually I’ve changed the line to $offlineEventsPage = $pages->children()->filterBy('template', 'offline-events')->first(); and now the prompt is gone. But it still shows no offline-event.
The page hierarchy is like this: Schulungen / Offline / Offline Seminar#1
Schulungen has the default template, offline has offline-events and the event itself has offline-event as template.

/edit: The offline-events.de.txt is actually not being filled with info when I create an offline-event as subpage.

First of all, if you cannot do proper debugging with xDebug, do the simple stuff:

dump($pages->children()->filterBy('template', 'offline-events')->first());
dump($offlineEventsPage->children()->toArray());

etc.

Other things to look into: Do you have duplicate page uuids? Duplicate text files?

I cannot debug this for you remotely…

I guess the unique uuid-thing was the problem. I’ve just recreated the page and suddenly it works as intended.
The key-problem was, that I forgot the “children()->” when calling(?) the template.
I am very thankful for this awesome support texnixe!

1 Like