Menu with fields

Hi there,

I am trying to build a menu with pages belonging to the field “jahr” and then “2015”, “2014”…

----
Jahr: 2015
----

But everything I tried did not work out…

<?php
$jahrseite = $pages->filterBy('jahr', '!=', '15')->visible();
if ($jahrseite) : 
foreach($children as $child): 
php echo html($child->nummer()) ?>
<br /><?php echo ($child->titelaus()) ?>

But there are no Pages to display.

Is there a smart solution to build a menu with all pages from the site that use this custom field?

Thanks, Tobias

I don’t really understand what you are trying to do. Do you want to get all children of the current page that have the field jahr set to 2015?

And what are the $children here, seems they are not defined?

Sorry for the big confusion!

I tried so many things all the day that I have gone totally insane.

My new approach - and this time it is working:

if ($child->jahr()=="2015") : 

This was the part I missed during reading the CheatSheet and the forum pages.

I even kann combine it with other fields in my pages :wink:

The whole filterBy and findBy approaches did not work out for me …

This is what worked for me:

$seiten = $pages->find('*MyTemplate*')->children(); 
foreach($seiten as $child): 
if ($child->bereich()=="1" AND $child->jahr()=="2015") : ?>
<li ><a href="<?php echo $child->url() ?>"><?php echo ($child->titel()) ?></li>
...

Greetings, Tobias

@tobiashartmann: pls. use three backticks to start and end a code block, so that your code is easier to read.

1 Like

Instead of

$seiten = $pages->find('MyTemplate')->children(); 
foreach($seiten as $child): 
if ($child->bereich()=="1" AND $child->jahr()=="2015") : ?>

you could also use a custom filter:

$seiten = $pages->find('MyTemplate')->children()->filter(function($p) {
  return $p->bereich()=="1" and $p->jahr()=="2015";
});
foreach($seiten as $child):
1 Like

Or you could use the following:

$seiten = $pages->find('MyTemplate')->children()->filterBy('bereich', '1')->filterBy('jahr', '2015'); 
foreach($seiten as $child): 
1 Like

Works fine!

Thank you very much!

What I don’t understand at the moment:

if ($seiten) :

doen’t work with this approach - now how can I prevent the Headline from beeing displayed?

 <?php
   $seiten = $pages->find('ausstellungen')->children()->filter(function($p) {
	return $p->bereich()=="1" and $p->jahr()=="2016";
	});
	if ($seiten) :?>
	<ul >
    <li>2016<ul>
    <?php
	foreach($seiten as $child):
	?>

Thanks for your support - I am feeling totally useless today!

You can use if($seiten->count() > 0).

Please note that you need to close the if-clause after that. Indentation is key and will make this clear very easily:

 <?php
 $seiten = $pages->find('ausstellungen')->children()->filter(function($p) {
  return $p->bereich()=="1" and $p->jahr()=="2016";
});
if ($seiten->count() > 0): ?>
  <ul>
    <li>2016<ul>
    <?php foreach($seiten as $child): ?>
    ...
    <?php endforeach; ?>
  </ul>
<?php endif; ?>
1 Like

You could use $seiten->count() > 0. (Edit: @lukasbestle was faster on this one)

Although I am wondering if ->groupBy() wouldn’t be a better way for you (only coming with the new version 2.2 - currently in beta):

$years = page('ausstellungen')->children()->filterBy('bereich', '1')->groupBy('jahr');
foreach($years as $year => $items): ?>
  <ul>
    <li><?php echo $year ?><ul>
      <?php foreach($items as $item) : ?>
      …
      <?php endforeach; ?>
    </ul></li>
</ul>
<?php endforeach ?>

You would not need to have blocks for each year with pretty much the same code in your template. Easier to maintain and make changes. I haven’t run this code, so there might be some small bugs, but it should work as a general idea of how to use groupBy

3 Likes

Looking forward to 2.2 :smile:

Thanks for your support!