mafleig
February 5, 2019, 12:18pm
1
Can I create a link to a page where the current page ist selected in a structure field?
i.e.: if page is selected, show a link to the page of selection.
the structure field looks like this:
sammlung:
label: sammlung
type: structure
fields:
bild:
label: Bild
type: select
required: true
options: query
query:
page: bibliothek/werke
fetch: children
value: '{{uri}}'
text: '{{title}}'
image: '{{image}}'
kuenstler: '{{kuenstler}}'
texnixe
February 5, 2019, 12:27pm
2
Yes, you can find the page with the filter($callback)
method. Inside the callback, use findBy()
on the structure field. Something like this:
$linkPage = page('whateverparentpageyouwanttofilter')->children()->filter(function($child) use($page) {
return $child->sammlung()->toStructure()->findBy('bild', $page->uri());
});
mafleig
February 6, 2019, 11:13am
3
very nice, the link already works, but the ->title() doesn´t appear so far:
<?php $linkPage = page('magazin')->children()->filter(function($child) use($page) {
return $child->sammlung()->toStructure()->findBy('bild', $page->uri());
});
?>
<a href="<?= url($linkPage) ?>"> <?= $linkPage->title() ?> </a>
magazin
is located here:
mypage/sammlungen/sammlung
the lpages with the link are here:
mypage/bibliothek/werke/bild
texnixe
February 6, 2019, 11:33am
4
mafleig:
page(‘magazin’)
The page helper needs the full path to the page!
mafleig
February 6, 2019, 12:26pm
5
sorry, i mixed up something:
magazin
is located here:
mypage/magazin/sammlung
the pages with the link are here:
mypage/bibliothek/werke/bild
page(‘magazin’) already leads to the right child(echoes: magazin/sammlung) , but the title doesn´t appear.
But I don’t understand why you are filtering the magazine page, when you are selecting from the /bibliothek/werke
page?
mafleig
February 7, 2019, 12:41pm
7
Well, it´s a bit confusing.
at first i need to check if the “bild” page of
mypage/bibliothek/werke/bild
is linked inside of a “sammlung” page
mypage/magazin/sammlung
.
If so, i want to get a link named with the title of “sammlung”.
with the suggested code it almost works, except for the title.
I don’t know where to fix it, because the link seems to work. well, the url($linkPage)
part does.
texnixe
February 8, 2019, 10:20am
9
What do you get when you do a
dump($linkPage);
Could you please post the result here?
texnixe
February 8, 2019, 10:44am
11
That look strange, twice the same? But I see where the problem is, it returns a collection of pages, not a single page object, so you either have to loop through $linkPage
or use first()
to get the first object, depending on your use case.
mafleig
February 8, 2019, 11:13am
12
that was it
works smooth now:
<?php $linkPages = page('magazin')->children()->filter(function($child) use($page) {
return $child->sammlung()->toStructure()->findBy('bild', $page->uri()); });
?>
<?php foreach($linkPages as $linkPage): ?>
<a href="<?= url($linkPage) ?>"> <?= $linkPage->title() ?></a>
<?php endforeach ?>