Skip to content

Releases: johannschopplich/kirby-headless

v2.2.0

09 Oct 09:26
Compare
Choose a tag to compare

   🚀 Features

    View changes on GitHub

v2.1.1

03 Oct 14:43
Compare
Choose a tag to compare

No significant changes

    View changes on GitHub

v2.1.0

03 Oct 09:13
Compare
Choose a tag to compare

   🚀 Features

    View changes on GitHub

v2.0.0

28 Sep 10:57
Compare
Choose a tag to compare

Warning

This release contains a breaking change. If you're using custom file resolvers for the toResolvedBlocks() method, please upgrade to the latest syntax below. If you haven't defined any blocksResolver options in your site/config.php, then you're good to go already!

Migration

toResolvedBlocks()

The toResolvedBlocks() method is a wrapper around the toBlocks() method. It's primarily intended for usage with KQL queries, because the toBlocks() method returns only UUIDs for the files and pages fields.

This field method will resolve the UUIDs to the actual file or page objects, so you can access their properties directly in your frontend.

# /site/config/config.php
return [
    'blocksResolver' => [
        // Define which fields of which blocks need resolving
        'files' => [
            // Resolve the `image` field in the `image` block as a file
            'image' => ['image'],
            // Resolve the `image` field in the `intro` block as a file
            'intro' => ['image']
        ],
        'pages' => [
            // Resolve the `link` field in the `customBlock` block as a page
            'customBlock' => ['link']
        ]
    ]
];

For an example, take a look at the 🍫 Cacao Kit frontend.

Custom Files or Pages Resolver

To resolve image UUIDs to image objects, you can define a custom resolver in your config.php. By default, the following resolver is used:

$defaultResolver = fn (\Kirby\Cms\File $image) => [
    'url' => $image->url(),
    'width' => $image->width(),
    'height' => $image->height(),
    'srcset' => $image->srcset(),
    'alt' => $image->alt()->value()
];

If you just need one custom resolver for all files fields, you can use the blocksResolver.defaultResolvers.files options key. Respectively, you can use the blocksResolver.defaultResolvers.pages options key for all pages fields.

Both options accept a closure that receives the file/page object as its first argument and returns an array of properties, just like the default resolver:

# /site/config/config.php
return [
    'blocksResolver' => [
        // Default Resolvers
        'defaultResolvers' => [
            'files' => fn (\Kirby\Cms\File $image) => [
                'url' => $image->url(),
                'alt' => $image->alt()->value()
            ],
            'pages' => fn (\Kirby\Cms\Page $page) => [
                // Default resolver for pages
            ]
        ]
    ]
];

Custom Resolver for a Specific Block and Field

If you need a custom resolver for images, links, or any other field in a specific block, you can use the blocksResolver.resolvers options key. It accepts an array of resolvers, where the key is the block name and the value is a closure that receives the field object as its first argument and returns an array of properties:

# /site/config/config.php
return [
    'blocksResolver' => [
        'resolvers' => [
            'intro:link' => fn (\Kirby\Cms\Field $field) => [
                'value' => $field->value()
            ]
        ]
    ]
];

   🚀 Features

    View changes on GitHub

v1.7.1

26 Sep 12:57
Compare
Choose a tag to compare

   🐞 Bug Fixes

    View changes on GitHub

v1.7.0

26 Sep 11:24
Compare
Choose a tag to compare

   🚀 Features

    View changes on GitHub

v1.6.3

07 Sep 08:02
Compare
Choose a tag to compare

No significant changes

    View changes on GitHub

v1.6.2

05 Sep 08:28
Compare
Choose a tag to compare

No significant changes

    View changes on GitHub

v1.6.1

15 Aug 07:44
Compare
Choose a tag to compare

   🚨 Breaking Changes

    View changes on GitHub

v1.6.0

01 Aug 20:28
Compare
Choose a tag to compare

   🚀 Features

   🐞 Bug Fixes

    View changes on GitHub