I have a field that is set with these keys+option names, that I am trying to get my code to format correctly to replace them. The GUI created field file:

But I am not managing to get that correct syntax, the same as above, by sending config_set an array, or a string.

I found https://docs.backdropcms.org/api/backdrop/core%21modules%21field%21modules%21list%21list.module/function/list_allowed_values_string/1 but that didn't format the array the same way either.

Accepted answer

Solved it by reading the list module code... Example here:

function torch_save_appstatus($stat=array()){

if(empty($stat)){

$stat = torch_get_status();

array_multisort(array_column($stat, 'order'), SORT_ASC, $stat);

}

$t=[];

// update the field settings for appstatus

if(!empty($stat)){

foreach($stat as $k=>$v){

$nm="";

if(!empty($v['parent'])){ // build child string

$nm = " - ";

}

$name=$nm.$v['name'];

$t+=[$v['id']=>$name]; //assoc array addition

}

 

$av= list_allowed_values_string($t); //list module function

$asv=list_extract_allowed_values($av, 'list_text', false);//list module function

 

config_set('field.field.field_appstatus', 'settings.allowed_values',$asv);//overwrite the field json

 

return $t;

}

}

Comments

Solved it by reading the list module code... Example here:

function torch_save_appstatus($stat=array()){

if(empty($stat)){

$stat = torch_get_status();

array_multisort(array_column($stat, 'order'), SORT_ASC, $stat);

}

$t=[];

// update the field settings for appstatus

if(!empty($stat)){

foreach($stat as $k=>$v){

$nm="";

if(!empty($v['parent'])){ // build child string

$nm = " - ";

}

$name=$nm.$v['name'];

$t+=[$v['id']=>$name]; //assoc array addition

}

 

$av= list_allowed_values_string($t); //list module function

$asv=list_extract_allowed_values($av, 'list_text', false);//list module function

 

config_set('field.field.field_appstatus', 'settings.allowed_values',$asv);//overwrite the field json

 

return $t;

}

}

So it's a two-step process: 

$av= list_allowed_values_string($t); // where $t is the assoc array of keys=>values

$asv=list_extract_allowed_values($av, 'list_text', false);//formats correctly for a list options form object