Problem with rwmb_get_value

Support General Problem with rwmb_get_value

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14731
    Camilo MejiaCamilo Mejia
    Participant

    I have a field like this:

    
    array(
      'type' => 'checkbox_list',
      'options' => array(
        'some-value' => 'Some value',
        'another-value' => 'Another value'
      )
    )
    

    rwmb_the_value() returns the values as an html ul and li list while rwmb_get_value() returns an array of keys instead of values. According to the documentation, that helper must returns an array of values, not the keys.

    I need to format the values with other html tags, but with the given helpers, there is not an option to get 'Some value' and 'Another value' in an array instead of pre formatted in html. rwmb_get_value() only gives me an array with 'some-value' and 'another-value'.

    #14744
    Anh TranAnh Tran
    Keymaster

    Hi Camilo,

    Actually, you are setting the field in opposite. The correct pattern should be:

    'value1' => 'Label 1',
    'value2' => 'Label 2'

    (It's the 'value' => 'Label', not 'key' => 'value').

    And the helper function returns the values, not labels.

    You can get the labels, you can do this:

    $field = rwmb_get_field_settings( 'field_id' );
    $options = $field['options'];
    
    // Get all labels.
    $values = rwmb_meta( 'field_id' );
    $labels = array_map( function( $value ) use ( $options ) {
        return $options[$value];
    }, $values );
    
    // Output labels.
    foreach ( $labels as $label ) {
        echo $label;
    }
    #14768
    Camilo MejiaCamilo Mejia
    Participant

    Hi Anh

    Thanks for your help, after looking how rwmb_the_value works in the plugin code I found rwmb_get_field_settings and implemented something similar to your example. I missed it when I looked in the documentation.

    But I think it's confusing that rwmb_the_value returns labels and rwmb_get_value returns values, when both methods have similar names. What about adding an optional parameter to rwmb_get_value to choose between labels and values? Or create rwmb_get_label to get labels without html?

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.