Hello everyone,
i’m trying to merge 2 fields together. One hidden field
settingcolorsdefault:
type: hidden
default: 1st, 2nd, Accent, Success, Info, Warning, Error, White, Light, Gray, Dark, Black
and one where the users can create additional colors
settingcolorscustom:
label: Benutzerdefinierte
type: tags
icon: settings
width: 3/4
i tried it with a collection, but don’t know if it is the proper way.
lcomposerbgco:
label: 'Hintergrund [--]'
type: select
options: query
query:
fetch: kirby.collection("colors")
text: '{{ arrayItem.value }}'
value: '{{ arrayItem.value }}'
width: 1/4
I had some problems with the collection, didn’t found the right way in kirby and made some workarounds, which didn’t work perfect.
collections/colors.php
<?php
require_once('site/snippets/_includes/getArrayByVariable.php');
return function ($kirby, $site) {
$defaultVariable = 'settingcolorsdefault';
$customVariable = 'settingcolorscustom';
return mergeArraysByVariables($site, $kirby, $defaultVariable, $customVariable, false);
};
getArrayByVariable.php
<?php
require_once('site/snippets/_includes/checkArray.php');
require_once('site/snippets/_includes/getCurrentPathFromKirbyObject.php');
function getCurrentPathFromKirbyObject($kirby){
$urlFromKirby = $kirby->urls()->current(); // gibt http://127.0.0.1:8000/api/pages/technikdirekt+test2/sections/content-col-0-fields oder http://127.0.0.1:8000/api/pages/technikdirekt+test2
$extracedShopAndSite = str_replace("pages/", "", substr($urlFromKirby, strpos($urlFromKirby, "pages/"))); // gibt technikdirekt+test2/sections/content-col-0-fields oder technikdirekt+test2
if (strpos($extracedShopAndSite, "/")){
$shopAndSitePath = str_replace("+", "/", substr($extracedShopAndSite, 0, strpos($extracedShopAndSite, "/"))); // gibt technikdirekt/test2
}else{
$shopAndSitePath = str_replace("+", "/", $extracedShopAndSite); // gibt technikdirekt/test2
}
return $shopAndSitePath;
}
function mergeArraysByVariables($site, $kirby, $defaultVariable, $customVariable, $isNotHex)
{
$shopAndSitePath = getCurrentPathFromKirbyObject($kirby);
$defaultArray = $site->findPageOrDraft($shopAndSitePath)->$defaultVariable()->split();
$customArray = $site->findPageOrDraft($shopAndSitePath)->$customVariable()->split();
if ($isNotHex) {
$customArray = checkIfCustomEntriesAreProper($customArray);
}
return A::merge($defaultArray, $customArray);
};
In addition i noticed, that my draft pages are not found.
TLDR:
i want to merge 2 fields (one already declared and one custom input field) into one dropdown with the data of both fields as options.
When my site is still a draft, i get this errorMessage

and i also have problems with the duplicate function.
I tried to return an object by adding (object) to my return, but this wasn’t working either. return (object) A::merge($defaultArray, $customArray);
I also tried to work without the ->split() because this returns an array
Thanks in advance.
Stefan