acf-builder icon indicating copy to clipboard operation
acf-builder copied to clipboard

Select 'return_format' only returns label

Open astray-heavy-arms opened this issue 4 years ago • 7 comments

In a similar vein to this: -

https://github.com/StoutLogic/acf-builder/issues/81

I create a select:-

$field_settings = [

     'choices' => [
      'value' => 'label', 
    ],
    'return_format' => 'value',
];

->addSelect('select_field', $field_settings);

Apologies for sudo code but the result I expect to see is value but what's returned is label and even when set to both it still returns label.

I understand there is this option to add choices.

->addChoices(['red' => 'Red'],

Feel free to guide me in the proper way to do this cause I'm a bit lost.

astray-heavy-arms avatar Oct 01 '20 20:10 astray-heavy-arms

@neverin772 I'm not sure what the issue is here. When you print_r the results of ->build() is return_format => 'value' in there still?

Looking at ACF docs, the proper setting for both is array not sure if that makes a difference though with getting this to work.

stevep avatar Oct 01 '20 20:10 stevep

I've come across the same (or at least a very similar) issue and wanted to chip in:

When setting up choices like

->addSelect('my_field', [
    'choices' => [
      1 => 'Label',
      2 => 'Something',
      3 => 'Else'
    ],
  ])

then the (numerical) value is being disregarded and the label is used / forced. Same when 'stringifying':

->addSelect('my_field', [
    'choices' => [
      '1' => 'Label',
      '2' => 'Something',
      '3' => 'Else'
    ],
  ])

But when setting via

->addChoices(1 => 'Label')

then the select / dropdown field correctly outputs 1 as option value for Label.

Regards!

E-VANCE avatar Jan 14 '21 09:01 E-VANCE

I had the same issue. I then dug deep into the add_choices method and found the issue.

`public function addChoices($choices) { if (func_num_args() > 1) { $choices = func_get_args(); } foreach ($choices as $key => $value) {

        $label = $choice = $value;

        if (is_array($choice)) {
            $label  = array_values( $choice )[0];
            $choice = array_keys( $choice )[0];
        }
         else if (is_string($key)) {
            $choice = $key;
            $label = $value;
        }
       

        $this->addChoice($choice, $label);
    }

    return $this;
}

`

In my opinion $choice can never be an array.

if (is_array($choice)) { $label = array_values( $choice )[0]; $choice = array_keys( $choice )[0]; }

This will never be run. And if your value in they ['value' => 'label'] pair is not a string the else if also doesn't run which is why you and up with addChoice($label, $label).

For me this fixed:

else if (is_string($key) || is_numeric($key)) { $choice = $key; $label = $value; }

calvinalkan avatar Jan 14 '21 22:01 calvinalkan

I have the same bug. I want to save post_ids as value to the options table, but can't since the post title gets put into the value instead of the post_id.

$pages = wp_list_pluck(get_posts('post_type=page&orderby=post_title&order=ASC&lang=fr&posts_per_page=-1'), 'post_title', 'ID');
$options
  ->addSelect('politique_page', array(
      'label' => __("Page de la politique d'adhésion", 'pix'),
      'choices' => $pages
  ));

nebulousGirl avatar Feb 17 '22 15:02 nebulousGirl

I am also facing this issue. How are you guys keeping choices with numeric value?

ernilambar avatar Mar 17 '23 11:03 ernilambar

I find little weird but it works this way.

->addSelect('featured_page', array(
  'choices' => [ [ '1' => 'One' ], [ '2' => 'Two' ] ]
) )

ernilambar avatar Mar 17 '23 11:03 ernilambar