Hey there,
I just tried to merge structure fields from multiple pages. But I can’t get it to work. Combining multiple pages works with “->add(…)” but for structures it doesn’t.
My page structure:
- Unimportant Page
- News page called "Aktuell"
2.1 News category called "Allgemeine Neuigkeiten"
2.2 News category called "Messen"
2.3 News category called “Termine”
- Another page
- (…)
In each children of the news page there is a structured field with news elements. Now I’d like to display the 3 most recent news on the home page. No matter what category.
That’s how my code looks:
<?
$neuigkeiten = $site->index()->findBy("uid", "aktuell")->children()->visible()->first()->news()->toStructure();
foreach ($site->index()->findBy("uid", "aktuell")->children()->offset(1) as $subaktuelle):
$neuigkeiten->add($subaktuelle->news()->toStructure());
endforeach;
foreach ($neuigkeiten->sortBy('date', 'desc') as $neuigkeit): ?>
<a class="neuigkeit">
<span class="datum"><?= $neuigkeit->date('d.m.y') ?></span>
<h3><?= $neuigkeit->headline()->kirbytextRaw() ?></h3>
<?= excerpt($neuigkeit->text(), 100) ?>
</a>
<? endforeach ?>
Thank you!
The following should work (but is untested):
<?php
$newsPages = $site->index()->findBy("uid", "aktuell")->children()->visible();
$news = new Structure();
foreach($newsPages as $p) {
foreach($p->news()->toStructure() as $key => $value) {
$news->append($key, structure($value, $p, $key));
}
}
foreach($news->sortBy('date', 'desc') as $item): ?>
...
Thanks for your reply!
I tested the code you suggested. Sadly, it only displays the structure elements from the first subpage and somehow isn’t a true structure field after the "->append()"s. Also, the fields in the structure field element each contain the whole structure field element. Really weird.
That’s how my code looks after your changes:
<?
$subaktuelles = $site->index()->findBy("uid", "aktuell")->children()->visible();
$neuigkeiten = new Structure();
foreach($subaktuelles as $p) {
foreach($p->news()->toStructure() as $key => $value) {
$neuigkeiten->append($key, structure($value, $p, $key));
}
}
foreach ($neuigkeiten->sortBy('date', 'desc') as $neuigkeit): ?>
<a class="neuigkeit">
<span class="datum"><?= $neuigkeit->date('d.m.y') ?></span>
<h3><?= $neuigkeit->headline()->kirbytextRaw() ?></h3>
<?= excerpt($neuigkeit->text(), 100) ?>
</a>
<? endforeach ?>
And that’s how it then looks in the source code:
My first version only displayed news of the first child too, but the fields were properly displayed at least:
Maybe I need to convert the structure field to an array to append stuff?
I tried using collection::toArray and a::merge($array1, $array2) but it didn’t work either.
Oh, and if it helps—here are screenshots from the panel viewing two news categories:
The first news category:
The second news category:
This news called “Mega aktuelle Messe” should be displayed first because the date (not displayed on the structure field entry…) is set in the future.
And what happens if you use this code?
<?php
$subaktuelles = $site->index()->findBy("uid", "aktuell")->children()->visible();
$neuigkeiten = new Structure();
foreach($subaktuelles as $p) {
$key = 0;
foreach($p->news()->toStructure() as $item) {
$neuigkeiten->append($key, $item);
$key++;
}
}
foreach ($neuigkeiten->sortBy('date', 'desc') as $neuigkeit): ?>
Ok, maybe this helps:
I’ve added a few other echos for debugging:
<?
$subaktuelles = $site->index()->findBy("uid", "aktuell")->children()->visible();
$neuigkeiten = new Structure();
foreach($subaktuelles as $p) {
$key = 0;
echo "<p>appended from \"" . $p->title() . "\":<br/>";
foreach($p->news()->toStructure() as $item) {
$neuigkeiten->append($key, $item);
echo $item->date('d.m.y') . " – " . excerpt($item->headline(), 25) . "<br/>";
$key++;
}
echo "</p>";
}
echo "<hr/>";
foreach ($neuigkeiten as $neuigkeit): ?>
<?= $neuigkeit->date('d.m.y') . " – " . excerpt($neuigkeit->headline(), 25) . "<br/>"?>
<? endforeach ?>
This results in:
So it looks like the structure fields are appended, but later not displayed.
EDIT:
So it was a really simple error in your suggested code. The “$key” variable resets itself after every page:
foreach($subaktuelles as $p) {
$key = 0;
That works:
$key = 0;
foreach($subaktuelles as $p) {
Thank you either way!
Oh, yes. Thanks for the clarification.
Here is the complete code for the others:
<?php
$subaktuelles = $site->index()->findBy("uid", "aktuell")->children()->visible();
$neuigkeiten = new Structure();
$key = 0;
foreach($subaktuelles as $p) {
foreach($p->news()->toStructure() as $item) {
$neuigkeiten->append($key, $item);
$key++;
}
}
foreach ($neuigkeiten->sortBy('date', 'desc') as $neuigkeit): ?>
But it just came to my mind that it could even be simplified. Also untested, but I think this should work just fine:
<?php
$subaktuelles = $site->index()->findBy("uid", "aktuell")->children()->visible();
$neuigkeiten = new Structure();
foreach($subaktuelles as $p) {
$neuigkeiten = $neuigkeiten->merge($p);
}
foreach($neuigkeiten->sortBy('date', 'desc') as $neuigkeit): ?>
For anyone who comes across this post: The first example with the append()
method works, the second one with the merge()
method does not.