Hi there,
In my controller, I am trying to pre-sort some structure fields. During this process, I need to add some virtual fields into my structure fields (result of some date calculations etc.).
I can achieve this like this:
<?php
return function ($page) {
$testStructures = $page->myStructureField()->toStructure()->map(function($s) {
$s->testfield = 'testString1234';
return $s;
});
return compact('testStructures');
};
Now, I can use this in my template like this and it will correctly insert the field’s string:
<p><?= $testStructures->first()->testfield() ?> </p>
However when I try to use a kirbytextinline()
function on this, it gives me the error:
Call to a member function kirbytextinline() on string
Is there a way I can avoid this and somehow insert this new field as a proper field into the structure, so I can still use the kirbytext functions when I use it in the template?
Strangely, I got it working with this old code I had flying around, but it seems to unneccessarily use a groupBy()
and a filter()
command and I need to write the fields into an array. If I don’t exactly set it up in this combination, it does not work. Since this is really confusing and complicated, I am looking for an easier alternative.
Here is the version that strangely works:
return function ($page) {
$testStructures = $page->myStructureField()->toStructure()->filter(function(){return true;});
$testGroups = null;
if($testStructures->isNotEmpty()) {
$testGroups = $testStructures->groupBy('_uid')->map(function($g) {
$g->myData = [
'testString' => 'testString1234'
];
return $g;
});
};
return compact('testGroups');
};
In my template I can then use the field like this:
<p> <?= $testGroups->first()->myData->testString()->kirbytextinline() ?> </p>
I have no idea why this works and I would be really happy, if someone could show me a simpler way.
Thanks!