You’ve probably saved the Builder values as yaml so you need to replace json
with yaml
in the toData()
methods.
We’ve adopted the above script to be executed as a CLI script and also reimplemented the convert function so it also works with Kirby 3.7. But this version lacks Support for converting Editor fields.
To run this, place this as convert_builder.php
and run it with php convert_builder.php
in the root of your Kirby installation. Please note: To convert nested Builder blocks you need to run this script with Kirby 3.6.
<?php
use Kirby\Toolkit\A;
require 'kirby/bootstrap.php';
$kirby = new Kirby();
$pages = site()->index(true);
$updated_contents = [];
function convertBuilderBlock(array $params): array
{
if (isset($params['_key']) === false) {
return $params;
}
$contentOut = [
'type' => $params['_key'],
];
unset($params['_uid']);
unset($params['_key']);
$contentOut['content'] = $params;
return $contentOut;
}
function migrateField($page, $name, $langCode = null)
{
if (!$page->content($langCode)->has($name)) {
return false;
};
$blocks = $page->content($langCode)->get($name)->toData('yaml');
$converted = [];
foreach ($blocks as $block) {
$converted[] = convertBuilderBlock($block);
}
if ($blocks === $converted) {
return false;
};
$page->update([ $name => $converted ], $langCode);
echo "Updated {$page->id()} → $name ($langCode)\n";
return true;
}
$kirby->impersonate('kirby');
$count = 0;
foreach ($pages as $page) {
$block_fields = A::filter($page->blueprint()->fields(), fn($f) => $f['type'] === 'blocks');
foreach ($block_fields as $field) {
$name = $field['name'];
if (!$kirby->multilang()) {
migrateField($page, $name);
$count++;
continue;
}
foreach ($kirby->languages() as $lang) {
$langCode = $lang->code();
migrateField($page, $name, $langCode);
$count++;
}
}
}
echo "Migrated $count field contents to the native blocks format.\n";