Find Structure Item in Files

Hello everyone,

Is it possible to specify in a json list of all files in which structure-fields a file is featured?

In other words:
With the code below I get a list of all the files and infos I need, except in wich structure-fileds each file is featured in. (Sometimes, the same file is featured in two structure-fields but i)

Many thanks in advance for any help!

works.json.php:

$files = $pages->find('works')->files()->sortBy('year', 'asc')->flip(); 
$json = [];

foreach($files as $photograph) {

    $json[] = [
        'title' => (string)$photograph->title(),
        'urlmedium' => (string)$photograph->thumb('medium')->url(),
        'urlbig' => (string)$photograph->url(),
        'isportrait' => (bool)$photograph->toggle()->toBool(),
        'year' => (string)$photograph->year(),
        'id' => (string)$photograph->filename(),
    ];
  
  }
  
  echo json_encode($json);

What do you mean with “structure field”? In which item in a structure collection?

Yes exactly! :slight_smile: (Basically I’m trying to get the names of the Series for each File of the page)

Inside your loop you can filter your structure field by the given file:

$series = $page->structurefield()->toStructure()->filter(function($item) use($file){
    return $item->photographyfield()->toFiles()->has($file);
});

This will give you the items where the given file is used.

@moonwalk s hint helped a lot.
This was the solution:

$page = $pages->find('works');
$files = $page->files()->sortBy('year', 'desc');
$series = $page->series()->toStructure(); 

$json = [];

foreach($files as $photograph) {

    $feature = $series->filter(function($item) use($photograph){
      $image = $item->photographs()->toFiles()->filterBy("filename", "==", $photograph->filename());

    if($image->isNotEmpty()) {
      return true;
    } else {
       return false;
    }
     
});

    $json[] = [
        'title' => (string)$photograph->title(),
        'urlmedium' => (string)$photograph->thumb('medium')->url(),
        'urlbig' => (string)$photograph->thumb('big')->url(),
        'isportrait' => (bool)$photograph->toggle()->toBool(),
        'year' => (string)$photograph->year(),
        'id' => (string)$photograph->filename(),
        'series' => array_values($feature->map(fn($item) => [
          'seriesname' => $item->seriesname()->value(),
        ])->data()),
      ];
  
  }
  
  echo json_encode($json);