Getting inside structured content

My problem with structured content is that I cannot get access into the structure. When I try to find a page that has been selected via structured content in the panel, “find” doesn’t work… Know what I mean?


<?php $advocats = yaml( $page->advocat() ) ?>

<?php foreach($advocats as $advocat): ?>
  <a href="<?php echo $page->find( $advocat['selection'] )->url) ?>">
    <h3><?php echo $page->find( $advocat['selection'] )->title) ?><h3>
<?php endforeach ?>


    label: Anwälte
    type: structure
    entry: >
      Auswahl: {{selection}}
        label: Auswahl
        type: select
        options: pages

As you can see I am creating a list of pages in the panel where the user can select which other pages he want to link to. Some kind of internal links. However, the links should include data from their origin i. e. the origin’s title, image, url, etc.

When I try to go inside the selected item via <?php echo $page->find( $advocat['selection'] )->url) ?> i keep getting errors like “Call to a member function title() on a non-object”.

I’ve tried multiple setups but the one above seems to be the one that should work - But doesn’t. I’m really stuck here, maybe you guys can help :computer: :no_mouth:

Other idea using Tobias’ code, but no output at all…

<?php foreach($page->selection()->yaml() as $A): ?>

  <?php $a = $pages->find( $A['selection'] ); ?>

  <a href="<?php echo $a->url() ?>">
    <h3><?php echo $a->title() ?></h3>

<? endforeach ?>            

#brettvormkopf :cold_sweat:

$page->find() only finds subpages of the current page;
$pages->find() will only find pages on the first level;

You need to use $site->index()->findBy('uid', $advocat['selection']) if the related page can be anywhere.

So this really depends on where your related pages are located …

Edit: I changed the code above, because find() does not seem to work with index(). Also, using the index might be a bit too much of a good thing if you have many pages, so it might be a good idea to narrow the selection down?

Thanks for your reply, always appreciated :blush: Yeah, narrowing down the selection would definitely be better but at the moment I just want this thing to work, hehe.

Haven’t tried index() before, but I can see your point. So i changed the code to…

<?php $advocat = yaml($page->advocat()) ?>

<?php foreach($site->index()->findBy('uid', $advocat['selection']) as $selected): ?>
  <a href="<?php echo $selected->url() ?>">
    <h3><?php echo $selected->title() ?></h3>
<?php endforeach ?>

… But this causes an “Invalid argument supplied for foreach()”. A warning instead of an error though :grin: I know that I want to do is totally solvable but as said: #brettvormkopf

Maybe a “visualization” might help…


The yaml structure is located in “lorem”. Here the panel user selects a page, like “ipsum”, which is somewhere else in the site. I want to get data from “ipsum” to be shown in “lorem” :no_mouth:

Try sth. like this, I prefer the toStructure() method here:

<?php $advocates = $page->advocat()->toStructure(); foreach($advocates as $advocat): ?>
    <?php  $relatedPage = (string)$advocat->selection();?>
    <a href="<?php  echo $site->index()->findBy('uid', $relatedPage)->url() ?>">
      <?php echo $site->index()->findBy('uid', $relatedPage)->title() ?>
<?php endforeach ?>	
1 Like

That’s it :boom:

Thank you so much! Me and a good pal were trying toStructure() as well but we didn’t connect that with (string)$string. Phew, I feel really relieved now. Thanks again :blush: