Get page-fields from filtered tags

Hey Friends,

hope you feel all well! Here ist my next problem :wink:

I want to check if there is a tag equal to another output. This works well.
If the tag is equal I want to output more informations from the page the tag is coming from.
I don’t find any way to figure out the right code… ;I

My Controller:

 <?php return function($site, $pages, $page) {
   $locations = $site->index()->filterBy('template', 'location'); 
   if($tag = param('tag')) {$locations = $locations->filterBy('tags', $tag, ',');}
   $tags = $locations->pluck('tags', ',', false);
   return compact('locations', 'tags', 'tag'); } ?>

In my Template:

<?php foreach($tags as $tag): ?>
	<?php if($tag == $event_ort): ?>

		<?php echo $tag ?>
        // this is the place i would output the page-title and other fields from page
        // <?php echo $my_content = $tag->… ?>

	<?php endif ?>			
<?php endforeach ?>

Like last times I hope you can give me a hint.
Regards
Martin

Yes, but from which page? Your tags seem to come from all over the place, i.e. $site->index(). If you pluck the unique tags from all these pages, you get a simple array of tags that are devoid of any information about the page you plucked them from.

Hey texnixe,

the tags come from exactly 2 different paths.

content/4-programm/struktur/locations/berlin/->childrens
//and
content/4-programm/struktur/locations/potsdam/->childrens

In my template I am sorting events by there locations via select.

content/4-programm/filme/->grandchildrens

This event-blueprint got its select-values from locations-pages.

  ort_sreening:
    label: Spielstätte
    type: select
    required: true
    options: query
    query:
      template: location
      fetch: pages
      value: '{{title}}'
      text: '{{title}}'

In my template

<?php $events = page('programm/filme')->grandchildren()->visible(); ?>
// get all events

<?php foreach ($events->sortBy('ort_sreening', 'asc')->pluck('ort_sreening', null, true) as $event_ort): ?>
// output all location
<h1><?= $event_ort ?></h1>

<?php foreach($tags as $tag): ?>
	<?php if($tag == $event_ort): ?>

		<?php echo $tag ?>
        // this is the place i would output the page-title and other fields from page
        // <?php echo $my_content = $tag->… ?>

	<?php endif ?>			
<?php endforeach ?>

<?php foreach($events->filterBy('ort_sreening', $event_ort->toString())->sortBy('date', 'asc') as $event) : ?>
<?php $movie = $event->parent() ?>
// output all movies by there equal location

Maybe the way is wrong with tags to get informations from locations page. But I don’t find any other way for myself.

Thank you in advance!
Martin

I guess you mean the title etc, of each event in the $events collection?

I assume what you really want to achieve here is to group your events by location? In any case with your current approach it will not work at all, because neither your tags array nor your locations array have any connection with the page.

Right, group events by there location. This works fine.
But now I want to output more fields from location page like text, title etc.

For the moment the information to group comes from the event-page via select…
Thats the problem I think.

The solution via tags was just an experiment :wink:

First of all, I’d use Kirby’s group function, then you have each event in the second loop, can find the location page from the value of the ort_screening field of the event and thus have access to everything that is stored in the location page.

<?php
$groupedEvents = $events->sortBy('ort_sreening', 'asc')->groupBy('ort_screening');
foreach ( $groupedEvents as $location => $events ) : 
?>
  <h1><?= $location ?></h1>
  <?php foreach ( $events as $event ) : ?>
    <?= $event->title() ?>
    <?php if ( $location = $site->index()->filterBy('template', 'location')->findBy('title', $event->ort_screening()->value()) ) : ?>
      <?= echo $location->title(); /* or whatever information you want to get from the location page*/ ?>
    <?php endif ?>
  <?php endforeach ?>
<?php endforeach ?>

Try to prevent searching the $site->index() by narrowing down the path to the locations.

<?php if ( $location = $site->index()->filterBy('template', 'location')->findBy('title', $event_ort->ort_screening()->value()) ) : ?>
	<?php echo $location->/* what_ever_informations*/; ?>
<?php endif ?>

Thats the magic!
I would cuddle you ;D

Best regards
Martin