2

I have a custom field called releaseDate in one of my entry types. I would like to use Element API to get all the distinct years assigned to those entries. I dump all the years for all entries just fine like this:

'endpoints' => [
    'api/v1/release_years' => function () {
    return [
        'class' => EntryResource::class,
        'elementType' => Entry::class,
        'criteria' => [
            'section' => 'products',
            'orderBy' => 'field_releaseDate',
            'where' => 'field_releaseDate IS NOT NULL',
//          'groupBy' => '????????'
        ],
        'transformer' => function (Entry $entry) {
            return [$entry->releaseDate->format('Y')];
        },
        'pretty' => true
    ];
}

But the minute I add a group clause to the criteria section, it fails. I've tried the following:

'groupBy' => 'YEAR(field_relaseDate)'

'groupBy' => 'YEAR(relaseDate)'

'groupBy' => 'field_releaseDate.year'

'groupBy' => 'releaseDate.year'

'group' => 'YEAR(field_relaseDate)'

'group' => 'YEAR(relaseDate)'

'group' => 'field_releaseDate.year'

'group' => 'releaseDate.year'

Any ideas if this is even possible? Also does anyone know if you can return a single array from Element API instead of a multi-level array? This output seems a bit inefficient:

'data' = [
    [2010],
    [2011],
    [2012],
    [2013],
    [2014],
    [2015],
    [2016],
    [2017],
    [2018],
    [2019],
]

I would much prefer:

'data' = [
    2011,
    2012,
    2013,
    2014,
    2015,
    2016,
    2017,
    2018,
    2019
]
  • Did you manage to solve this? – j00lz May 31 at 3:20
  • Nope. I ended up making another entry type for release years and using a hook to add data dynamically to it. It ended up working ok since we eventually had to add other meta data to the release years. – Tony DeStefano Jun 7 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.