StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
792 stars 60 forks source link

method to implement array of choices with numeric values #94

Closed roboparker closed 5 years ago

roboparker commented 5 years ago

It is difficult to add an array of key value pairs to the choice options of a field when the value is numeric because it gets converted to the string label.

An example is setting a select field that list the gravity forms with the id as the value and the name as the label.

foreach ( \RGFormsModel::get_forms(null,'title') as $form ) {
$choices[$form->id] = $form->title;
}

An array like this cant be added with the addChoice or addChoices or as the choices value in the select fields arg array. In all these cases the values are converted to match the labels.

The workaround for this is a little cumbersome because it breaks the chaining.

$job_applications = $job->addSelect('job_application_form', [
    'return_format' => 'array',
]);
foreach ( \RGFormsModel::get_forms(null,'title') as $form ) {
    $job_applications->addChoice($form->id, $form->title);
}

Can we add a new method or an arg to prevent converting the numeric values to match the string?

curtisbelt commented 5 years ago

@diaboloshogunate Is the solution in https://github.com/StoutLogic/acf-builder/issues/83 helpful?

roboparker commented 5 years ago

That works, thank you!