Combining files/blocks from parent and children pages

Hello,

On the home page, for each other page with the template location I am trying to display images merged from a particular field on that page, with images from any image block of its children pages.

I know that the code below is not going to do what I want because of the foreach loops, and that is where I start to get lost, extracting each image from each block of each child page. :grimacing:

<?php
  $locations = site()->index()->filterBy('intendedTemplate', 'location')->listed(); 
  foreach ($locations as $location): ?>
  <div>
    <?php
    $coll = new Collection();
    $locationImages = $location->siteImages()->toFiles();
    $responses = $location->children()->listed();
    foreach ($responses as $response) {
      $responseBlocks = $response->responseContent()->toBlocks()->filterBy('type','image');
      foreach ($responseBlocks as $responseBlock) {
        $responseImage = $responseBlock->image()->toFile();
      }
    };
    $coll = $coll->add($locationImages)->add($responseImage);
    ?>
  </div>
<?php endforeach ?>

Any help would be appreciated. :slight_smile:

Do you want all images of all these pages in one big collection or per location?

Iā€™m looking to display them per location.

Then try:

<?php
  $locations = site()->index()->filterBy('intendedTemplate', 'location')->listed();
  foreach ($locations as $location): ?>
  <div>
    <?php
    $locationImages = $location->siteImages()->toFiles();
    $responses = $location->children()->listed();
    foreach ($responses as $response) {
      $responseBlocks = $response->responseContent()->toBlocks()->filterBy('type','image');
      foreach ($responseBlocks as $responseBlock) {
        $locationImages->add($responseBlock->image()->toFile());
      }
    };
?>
  </div>
<?php endforeach ?>

That worked perfectly, thanks @texnixe