Filter matching entries from Pages-Field

Hey there,

hopefully there is someone who can clean up my code concerning matching pages-field entries to another page.

This is the Structure of the site.

  1. Home
  2. Program
    — event a
    — event b
    — event c
    — …
  3. Movies
    — movie a
    — movie b
    — movie c
    — …

Part of Event Blueprint:

category:
  label: Kategorie
  type: radio
  translate: false
  required: true
  width: 1/2
  options:
    single: Film
    multiple: Filmblock

movie:
  width: 1/2
  label: Filme
  empty: Film auswählen…
  type: pages
  multiple: false
  translate: false
  min: 1
  query: site.index.filterBy("template", "in", ["movie"])
  when:
    category: single

movies:
  width: 1/2
  label: Filme
  empty: Filme auswählen…
  type: pages
  translate: false
  min: 2
  query: site.index.filterBy("template", "in", ["movie"])
  when:
    category: multiple

Part of Movie Template (this is where I made waste code but it works).
I think via FilterBy the code could be cleaned…

<?php
// find eventspage
$EventsPage = $site->index()->findBy('intendedTemplate', 'schedule');
// fetch all events by date > now
$AllEvents = $EventsPage->children()->listed()->filterBy('eventdate', 'date >', 'now')->sortBy('eventdate', 'asc');
// fetch all events by date < now
$AllPastEvents = $EventsPage->children()->listed()->filterBy('eventdate', 'date <', 'now')->sortBy('eventdate', 'desc');
?>

<!-- start loop -->
<?php foreach($AllEvents as $Event): ?>

  <!-- check the event has single movie -->
  <?php if ($Event->category() == 'single'): $SingleMovie = $Event->movie()->toPage()->title(); ?>

    <!-- here starts the CHAOS – check matchings -->
    <?php if ($page->title()->value() === $SingleMovie->value()): ?>
       <!-- all the output I want to display -->
    <?php endif ?>

  <?php elseif ($Event->category() == 'multiple'): ?>

    <!-- CHAOS round2 – loop through pages-field -->
    <?php foreach($Event->movies()->toPages() as $MultipleMovie): ?>

      <!-- more CHAOS – check matchings round2 -->
      <?php if ($page->title()->value() === $MultipleMovie->title()->value()):?>
        <!-- all the output I want to display -->
      <?php endif ?>

    <?php endforeach ?>

  <?php endif ?>

<?php endforeach ?>

I’m a heathen, but if there is a code god, I’ll go straight to hell for what I wrote above… at least I think so…

Thank you in advance!
Martin

No one any idea? :heart_eyes:

I don’t understand why you make the difference between one or multiple movies in the first place, I’d just use one movies field.

But in any case, the code can be simplified (at least if the output is basically the same for one or multiple movies).

// fetch page directly instead of querying the complete index
$eventsPage = page('events'); 
$events     = new Pages();
// check if you have an object before trying to get the children
if ($eventsPage) {
	$events = $eventsPage
		->children()
		->listed()
		->filterBy('eventdate', 'date >', 'now')
		->sortBy('eventdate', 'asc');
}

// loop through the events, then convert either field to pages
foreach ($events as $event) {
    $movies = $event->category()->value() === 'single' ? $event->movie()->toPages() : $event->movies()->toPages() ;
    // filter by movies by current page
    $currentPageMovies = $movies->filter(fn($movie) => $movie === $page);
    foreach ($currentPageMovies as $movie) {
        // do stuff
    }
}