Edit: Sorry for the messy thread, it took a while to figure out the actual problem. For a concise description what is the actual problem, read post #6.
Hi there,
in a controller file, I try to presort and group some events of a theater group whose page I am working on. I have successfully done something like this in Kirby 2 in the past, but I am now failing in Kirby 3 and would need some help. Here’s what I did in Kirby 2:
<?php
return function($site, $pages, $page) {
$pastEvents = $page->events()->toStructure()->sortBy('date', 'asc')->filterBy('date', '<', time())->map(function($l) {
$eventArray = [];
if($l->context()->isNotEmpty()) $eventArray[] = $l->context();
if($l->location()->isNotEmpty()) $eventArray[] = $l->location();
$l->eventString = join(', ', $eventArray);
return $l;
})->groupBy('eventString')->map(function($g) {
// here happens some date parsing, does not really matter
// but provides the variables shown below ($rangeStrings etc.)
$g->dateString = join("<br>", $rangeStrings);
$g->eventString = "<br>" . $g->first()->eventString();
$g->earliestDate = $g->first()->date('Y-m-d');
return $g;
})->sortBy('earliestDate', 'asc');
Note the grouping via a virtual eventString
field and the creation of further virtual fields of the group object before the group is returned (sorry, if my terminology might be off, I created all this pretty much by poking in the dark, but eventually it did what I wanted it to do).
Now, when I do the same in Kirby 3, I can replicate most steps, but it fails at creating these virtual fields of the group object (which I guess is called a StructureObject internally in Kirby?). Here is an example that fails:
<?php
return function ($page) {
$pastEvents = $page->events()->toStructure()->sortBy('date', 'asc')->filter(function ($show) {
return $show->date()->toDate() < time();
})->map(function($l) {
$eventArray = [];
if($l->event()->isNotEmpty()) $eventArray[] = $l->event();
if($l->stage()->isNotEmpty()) $eventArray[] = $l->stage();
$l->eventString = join(', ', $eventArray);
return $l;
})->groupBy('eventString')->map(function($g) {
// again, some date parsing ...
$g->testField = "TEST";
return $g;
});
At the line where I try to create the test field, it fails with this error:
Argument 1 passed to Kirby\Cms\StructureObject::setContent() must be of the type array or null, string given, called in /Users/some/path/kirby/src/Toolkit/Properties.php on line 138
Has Kirby changed in that regard? Why can’t I write strings to these groups anymore? Is there any way around it? I guess I could create mini-arrays of length 1, just to access the only value, but that would be kind of ugly, I think.
Thanks for any help!