I figured out how to get database values into a multiselect field and display the according text values in the field. However what I cannot get to work is the preview in a table, which displays the pages that have the mentioned multiselect field. I only get the field values, where it should display the texts. Is there another custom method needed to get the value->text mapping for database values in a preview? I tried another plugin for the multiselect preview, but that also just gives me the values. I found another one with computed: property but I was hoping there is an existing out of the box solution and it’s just a missing configuration. Can someone shed light on this? Thank you!
plugin/index.php
<?php
use Kirby\Database\Db;
Kirby::plugin('test-plugin', [
'pageMethods' => [
'db-skills' => function () {
$skills = [];
foreach (Db::select('skills', ['id','name']) as $skill) {
$skills[]=$skill;
}
return $skills;
},
],
skills.yml
label: Skills
type: multiselect
options: query
query:
fetch: page.db-skills
value: "{{arrayItem.id}}"
text: "{{arrayItem.name}}"
module.yml
title: Module
pages: false
fields:
skills: fields/skills
modules.yml
title: Modules
pages: true
sections:
modules:
type: pages
template: module
empty: No modules yet
label: Modules
parent: site.find("modules")
layout: table
columns:
skills: true
You need a custom page method that returns the desired value.
Isn’t that the one I already have in the plugin? How would I integrate a different one for the preview into modules.yml?
OK, I think I got it. The value: attribute in the columns: parameters of the table layout for the pages section … pretty tricky to figure out, the docs weren’t particularly helpful here, or maybe too overwhelming. No clue where to look for display customizations of that table layout option …
plugin/index.php
'pageMethods' => [
'db-skills' => function () {
return iterator_to_array(Db::select('skills_no_roots'));
},
'db-skills-map' => function ($values_string) {
$values_array = explode(', ', $values_string);
$db_skills = Db::select('skills_no_roots')
->filterBy('id', 'in' , $values_array)
->pluck('name',',',true);
return join(', ', $db_skills);
},
],
modules.yml
title: Modules
pages: true
sections:
modules:
type: pages
template: module
empty: No modules yet
label: Modules
parent: site.find("modules")
layout: table
columns:
skills: true
type: tags
value: '{{page.db-skills-map(page.skills)}}'
@texnixe is there anything I could have used in out-of-the-box tools where the conversion from stored text data (page.skills) to array? Double declaration of separators seems error prone. Thank you!
This can be simplified a little
'db-skills-map' => function () {
$values_array = $this->skills()->split(',');
$db_skills = Db::select('skills_no_roots')
->filterBy('id', 'in' , $values_array)
->pluck('name',',',true);
return join(', ', $db_skills);
},
Then you don’t have to pass the param to it:
value: '{{page.db-skills-map}}'
@texnixe Thank you Sonja!