Custom Fields
Re-Envisioned
Support General Problem with rwmb_get_value

This topic contains 2 replies, has 2 voices, and was last updated by  Camilo Mejia 2 months, 3 weeks ago.

  • Creator
    Topic
  • #14731

    Camilo 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’.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #14744

    Anh 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 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 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.