Get pages from structure field

Hi,

I have a fairly simple structure field, with a pages field as one column (with multiple: false set) and a date field as another. So only one page can be selected per entry. This is the blueprint:

found:
  label: Found statues
  type: structure
  empty: No statues found yet
  fields:
    statue:
      type: pages
      multiple: false
      label: Statue
      image: false
      query: site.index.filterBy('template','gingerbread')
    scanned:
      type: date
      time:
        time: true
        display: HH:mm:ss
        step:
          unit: second
          size: 1
      label: Scanned
      help: This is the date and time the entrant scanned the statue
      display: DD/MM/YYYY

I also have a collection of pages.

I need to step through the collection of pages, and check each page to see if it appears in the pages in the structure, and if so to style it differently.

I am aware of the has() method, and am planning on using it like this:

<?php
foreach($collectionPages as $collectionPage):
  if($structurePages->has($collectionPage):
    // Different styling
  else:
    // Standard styling
  endif;
endforeach;
?>

However I am not sure how to get a pages object out of the structure field to work with.

If you could offer any guidance that would be great.

<?php
foreach($collectionPages as $collectionPage):
  $statues = $collectionPage->found()->toStructure()->pluck('statue', '-', true);
  if(in_array($collectionPage->uuid()->toString(), $statues)):
    // Different styling
  else:
    // Standard styling
  endif;
endforeach;

Or

$statues = pages($collectionPage->found()->toStructure()->pluck('statue', '-', true));
if ($statues->has($collectionPage)) {}

Hi Sonja,

Thanks for the quick reply and the pointer. I am focussing on the second method, as I am using a controller and it would be good to have it saved out as a variable.

I have this:

$foundStatues = pages($kirby->user()->found()->toStructure()->pluck('statue','-',true));

But that is outputting an empty pages object (the user in question has 3 entries in the structure). When I remove the pages() I get an empty array, so something isn’t quite right.

If I just do this:

$foundStatues = $kirby->user()->found()->toStructure()->pluck('statue')

I get the following, so it is finding the entries ok:

Array
(
    [0] => Kirby\Content\Field Object
        (
            [statue] => Array
                (
                    [0] => page://8DGCAssGvyvxx1cL
                )

        )

    [1] => Kirby\Content\Field Object
        (
            [statue] => Array
                (
                    [0] => page://lwHEspk8kTq4etcu
                )

        )

    [2] => Kirby\Content\Field Object
        (
            [statue] => Array
                (
                    [0] => page://3aCaxL4N91DYiHem
                )

        )

)

I think the issue is with how the structure field is being saved - it looks like this in the txt file:

Found:

- 
  statue:
    - page://8DGCAssGvyvxx1cL
  scanned: 2023-11-25 15:19:38
- 
  statue:
    - page://lwHEspk8kTq4etcu
  scanned: 2023-11-23 15:19:52
- 
  statue:
    - page://3aCaxL4N91DYiHem
  scanned: 2023-11-01 10:03:14

If I change it to this it works:

Found:

- 
  statue: - page://8DGCAssGvyvxx1cL
  scanned: 2023-11-25 15:19:38
- 
  statue: - page://lwHEspk8kTq4etcu
  scanned: 2023-11-23 15:19:52
- 
  statue: - page://3aCaxL4N91DYiHem
  scanned: 2023-11-01 10:03:14

I am not sure what to try next, do you have any thoughts?

EDIT: I am working in the V4 release candidate if that is relevant

What do you get if you use ->pluck('statue', ',', true)?

That should at least give you a nested array instead of an array of fields.

Hi,

I tried this:

$foundStatues = $kirby->user()->found()->toStructure()->pluck('statue', ',' ,true)

Output using print_r($foundStatues) is:

Array
(
    [0] => Array
)

If I remove the true I get this:

Array
(
    [0] => Array
    [1] => Array
    [2] => Array
)

I appreciate your help, I am not sure what to try next

$found      = kirby()->user()->found()->toStructure()->pluck('statue');
$result     = array_map(fn ($field) => $field->value()[0] ?? '', $found);
$foundPages = pages($result);

dump($foundPages);

That works! Thanks so much, I have the pages object populating and can check it against the full pages list.

Brilliant :dizzy: