Show if page is part of a structure field

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}}'

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());
});

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

The page helper needs the full path to the page!

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?

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.

What do you get when you do a

dump($linkPage);

Could you please post the result here?

59

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.

that was it :slight_smile:

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 ?>