Hi,
I’m using 3 and I have a large content directory that contains over 7000 radio shows. To improve the speed of the site a sqllite database was introduced that syncs the content directory into the db every time a new show is added via the panel.
Each show has a genretags file which is using Kirby’s multiselect field.
The problem I’d like to solve is I’d like to replace some of genres assinged to shows with new ones i.e Juke be replaces for Footwork - in does so each show in the content directory would have it’s genretags field updated to include Footwork in place of Juke. Then when the db migration script is next run, it will update the db record with the updated field.
Can someone advise on the best way to do a mass find & replace on content?
Since there are so many files, I’d do this on the command line.
Create a script (or a Kirby CLI command, see CLI | Kirby CMS Plugins).
In your script, you can filter all shows by this field value.
Then while looping through the result, fetch the field value into an array, replace the value, then save back a comma separate string to the page (using $page->update()):
Great- thanks for the response.
So far have this but the problem is the new value of the multi select field is wrong. I would like the value of genretags to stay the same only with Juke replaced with Footwork.
<?php
$kirby = kirby();
$kirby->impersonate('kirby');
return [
'description' => 'Migrate Genres',
'args' => [],
'command' => static function ($cli): void {
$shows = page('shows')->children()->index()->listed();
foreach($shows as $show) {
if($show->genretags()->value()) {
var_dump($show->genretags());
$show->update([
'genretags' => Str::replace($show->genretags(), 'Juke', 'Footwork')
]);
}
}
$cli->success();
}
];
I think this should be inside the command?
It seems to run fine without that inside the function scope.
I’m almost there - just need to know how to change the value of a multiselect before overwriting using the update command.
I wouldn’t do a string replacement, but convert the field value to an array, replace the value, then convert back to a comma separated string.