Hi,
I guess you want to dynamically create sortable fields. You need to define numerically indexed array elements for the field definition array for sortable items.
In your case, maybe,
$ArrayFields = array(
'label' => 'Apple',
'default' => 'a',
array(
'label' => 'Bananas',
'default' => 'b',
),
array(
'label' => 'Cherry',
'default' => 'c',
),
);
$this->addSettingFields(
array(
'field_id' => 'sortable_labels',
'type' => 'hidden',
'title' => __( 'Sortale Labels', 'admin-page-framework-example' ),
'sortable' => true,
) + $ArrayFields
);
Notice that the first sortable item (Apple) defined in the $ArrayFields array does not have a numeric index.
So just create your custom function to format the $ArrayFields array from your generated data.
Hope it helps.
Thread Starter
Javier
(@xarkitu)
Hey, this is great 🙂
This opens many doors for my coding.
//My array
$ArrayList = array(
'a' => 'Apple',
'b' => 'Bananas',
'c' => 'Cherry,
);
//My default sorteable list
foreach($ArrayList as $key => $lb){
$def[] = $key;
}
//Get the fields...
$first = true;
foreach($this->getOption('sortable_labels', $def ) as $option) {
if($first) {
$MyFields = array(
'label' => $ArrayList[$option],
'default' => $option,
);
$first = false;
} else {
$MyFields[] = array(
'label' => $ArrayList[$option],
'default' => $option,
);
}
}
//Publishing the field...
$this->addSettingFields(
array(
'field_id' => 'sortable_labels',
'type' => 'hidden',
'title' => __( 'Sortale Labels', 'admin-page-framework-example' ),
'sortable' => true,
) + $MyFields
);
Miunosoft thank you very much for your support, I hope this helps anyone seeking to codify something.
best regards
Glad to hear you found a way! Good luck!