How to create blocks from a route?

Hello,

I’m trying to create blocks from a route. I tried :

$blocks = new Blocks();
$blocks->add([
   'type' => 'file'
]);

But it doesn’t work since the Blocks class isn’t accessible from the config file. In any case, I don’t know how to use the $blocks->add() method. The reference only says that it waits for an object as param. But which kind of object ?

Have a look here: Quicktip: Add blocks to a blocks field programmatically | Kirby CMS

1 Like

Great ! Thank you for your answer.

There is no image block in @texnixe example. Do you know the structure ?
Something like this I guess ?

$image = new Kirby\Cms\Block([
'content' => [
      'url' => $page->file()->url()
   ],
   'type' => 'image'
]);

Have a look at the blueprint for the image block for the available fields: image | Kirby CMS

Note that the fields are different for external images and local images. Keep in mind that the image field expects an array.

1 Like