autofill a select custom field with a custom field from another group.

Support General autofill a select custom field with a custom field from another group.Resolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #35789
    Fernando ArbexFernando Arbex
    Participant

    Hello everyone,

    How to use a value from a custom field (with id: leader) of a group and use this leader as a select field in another group?

    In this scenario I'm building, there will be several groups that will first be created by the leader filling the group form.

    Then the members of that leader group will fill the member form and chose the leader. This leader will be the one that was filled when they create the group and I want it to be auto-populated whenever a new group is created.

    Any idea?

    #35797
    Long NguyenLong Nguyen
    Moderator

    Hi Fernando,

    This case is beyond the scope of support of Meta Box. Meta Box does not support populating a field value based on another field value. You can use JS code with AJAX to update the select options.

    #35803
    Fernando ArbexFernando Arbex
    Participant

    Hi Long,

    You mean, create a function in Code Snipets called function_name (for example), and call it in the select field?

    The select field has this message below:

    To use a PHP function that returns an array of options, enter callback: function_name.
    The callback function must be declared before adding to the box.

    So I would enter callback: function_name?

    Is that it?

    #35811
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, you can use the callback function to create dynamic options for the select field. But the options will be parsed on load, not "live" (realtime update) as JS does.

    #35820
    Fernando ArbexFernando Arbex
    Participant

    Hi long,

    Thank you for your answer, I'm trying to make it work but need a hand.

    Althought I have the direction I'm quite stucked on how to make it work.
    I found a few codes on the internet that does that and I modified for my scenario.
    Here is the code.

    <?php
    function autoPopulateLeader( $field ) {
    // reset lider_membro
    $field['lider_membro'] = array();
    // get the value of post_title (leader) from Custom Group with id
    // informacoes_celula without any formatting
    $choices = rwmb_get_value( 'post_title', 'informacoes_celula' );
    // explode the value so that each line is a new array piece
    $choices = explode("\n", $choices);
    // remove any unwanted white space
    $choices = array_map('trim', $choices);
    // loop through array and add to field 'lider_membro'
    if( is_array($choices) ) {
    foreach( $choices as $choice ) {
    $field['lider_membro'][ $choice ] = $choice;
    }
    }
    // return the field
    return $field;
    }

    Notes:
    - lider_membro is custom field (group ID membro) that I want to be populated from the post_title of the group ID informacoes_celula
    - post_title is the leader custom field in the group ID informacoes_celula
    - this code is in code snipet to load on all pages
    - in the custom field type for lider_membro I used Select and callback: autoPopulateLeader

    #35825
    Long NguyenLong Nguyen
    Moderator

    Hi,

    There are two notes in this case:

    1. The callback function should return an array of values and labels as setting options

    function my_custom_options() {
        $options = [
            'java'       => 'Java',
            'javascript' => 'JavaScript',
            'php'        => 'PHP',
        ];
        return $options;
    }

    register the field select

    [
        'name'            => 'Select',
        'id'              => 'select',
        'type'            => 'select',
        'multiple'        => true,
        'placeholder'     => 'Select an item',
        'select_all_none' => true,
        'options'         => my_custom_options(),
    ],

    2. The helper function rwmb_meta() runs after the filter hook rwmb_meta_boxes so it does not work with the callback function. You can use the function get_post_meta() to get field value inside the function callback.

    Refer to this topic https://support.metabox.io/topic/bug-settings-not-available-for-custom-fields

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