Grouping by Merged Collections

Hi,

I’m creating a timeline from a couple of collections that I’m combining. They share a field called year.

I’d like to group by decade. I can run the group callback on each individual collection, but when I combine them and run the group callback I get an error message: Invalid structure data

Code below:

timeline.php

<?php 
$p = $page; 
$tl = $p->timeline()->toStructure()->sortBy('year', 'asc'); // collection 1
$images = $p->timelineimages()->toFiles(); // collection 2

//create a new 'collection'
$timeline = new Structure();
$timeline->add($tl);
$timeline->add($images);

//sort
$timeline = $timeline->sortBy('year', 'asc');

$callback = function($y) {
  //get decades
  $dec = intdiv($y->year()->toInt(),10); //results in integer
  $dec *= 10;
  return $dec;
};

$tg_grouped = $timeline->group($callback); //error message
dump($tg_grouped);

?>

Page Blueprint
(pulling the structured field timeline above $tl = $p->timeline()->toStructure()->sortBy('year', 'asc'); )

title: Timeline
icon: ⌛️

fields:
  timeline:
    type: structure
    width: 2/3
    sortBy: year desc
    columns:
      year:
        width: 1/8
      description:
        width: 6/8
      tofix:
        width: 1/8
    fields:
      year:
        type: number
        label: Year
        width: 1/4
        required: true
      endyear:
        type: number
        label: End Year
        width: 1/4
        help: "Use only if entering a range"
      description:
        label: Description
        type: textarea
        width: 3/4
        required: true
      note:
        label: Note
        width: 3/4
        type: textarea
        help: "seen only internally"
      tofix:
        label: Needs Review
        width: 1/4
        type: toggle
        text: ["no", "yes"]
  timelineimages:
    label: Timeline Images
    type: files
    uploads:
      template: timeline
    layout: cards
    image:
      back: black
    size: tiny
    width: 1/3

File Blueprint
(pulling the files field timelineImages $images = $p->timelineimages()->toFiles(); in timeline.php above)

title: Timeline

columns:
  main:
    width: 1/2
    sections:
      content:
        type: fields
        fields:
          title:
            label: Caption
            type: text
          year:
            label: year
            type: number
            width: 1/4
          endyear:
            type: number
            label: End Year
            width: 1/4
            help: "Use only if entering a range"
          circa:
            label: Circa
            type: toggle
            text:
              - "no"
              - "yes"
            width: 1/4
          hideyear:
            label: Hide Year
            type: toggle
            text:
              - "no"
              - "yes"
            width: 1/4

setting

$timeline = new Collection();

instead of

$timeline = new Structure();

fixed this for me.

I’m curious as to why - I thought Structure and Collection are functionally similar?

They are similar: Structure extends Collection.
One of the differences is that it expects its items to be StructureObjects, you are giving it Files. I guess that’s why it complains about “Invalid structure data”.