• Resolved AWOL

    (@awol)


    Hi,

    My last (resolved) post on this was several months ago – https://www.ads-software.com/support/topic/select-list-of-users/ -but I have now run into a problem where it works on one site but not on another, although the code has been very slightly tweaked on the site where it’s not working. This was custom code generously provided by you, so I understand will not be a priority, but I would greatly appreciate any help. To assist I will provide the code that isn’t working, and highlight the difference between that and the working code.

    NOT WORKING CODE (IN MU-PLUGINS FOLDER)
    <?php
    add_filter( 'forminator_cform_render_fields', function ( $wrappers, $model_id ) {
        // Form id where this code will be applied
        if ( $model_id != 2280 || $model_id != 2300) {
            return $wrappers;
        }
        $user_id = get_current_user_id();
        $members = friends_get_friend_user_ids( $user_id );
            if ( ! empty( $members ) ) {
                $new_options = array();
                $names = array();
                foreach( $members['members'] as $member ) {
                    $new_options[] = array(
                        'label' => $member->user_login,
                        'value' => $member->user_login,
                        'limit' => '',
                        'key'   => forminator_unique_key(),
                );
            }
                $names['options'] = $new_options;
        }
            // Define select fields to be populated
        $select_fields_data = array(
            'select-1' => 'users',
        );
    
        // Look for individual fields or fields in groups
            foreach ( $wrappers as $wrapper_key => $wrapper ) {
                if ( ! isset( $wrapper[ 'fields' ] ) ) {
                    continue;
                }
                for ($index = 0; $index < count($wrapper['fields']); $index++) {
                    if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && $select_fields_data[ $wrapper[ 'fields' ][ $index][ 'element_id' ] ] === 'users'){
                            $select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][$index]['element_id'], true );
                                if ( $select_field ) {
                                    Forminator_API::update_form_field( $model_id, $wrapper['fields'][$index]['element_id'], $names );
                                    $wrappers[ $wrapper_key ][ 'fields' ][ $index ][ 'options' ] = $new_options;
                                }
                    }
                }
            }
        return $wrappers;
    }, 10, 2); 
    
    CODE FROM WORKING VERSION THAT IS DIFFERENT
        // Form id where this code will be applied
        if ( $model_id != 6451 ) {
            return $wrappers;
        }
        // Retrieve group member names
        $user_id = get_current_user_id();
        $grp_id = bp_get_current_group_id();
        $args = array( 'group_id' => $grp_id, 'exclude' => $user_id, 'exclude_admins_mods' => 0);
        $members = groups_get_group_members($args);
    

    With the code that’s not working, the dropdown field is there to be clicked, but nothing drops down. When I compare the actual created forms, they are identical (other than labels and descriptions) except the one that doesn’t work has one less field (a hidden field); it was imported from the other site then duplicated and edited. What I did notice though was that the options in the select field were filled with the users from the other site, and initially I hadn’t noticed this so thought the form was working with the filter. I deleted those and that is when I realised the filter wasn’t working. I have created a completely new form to see if the import and duplication was the issue, but the behaviour is the same. I have also tried removing the if condition with the form ids. It seems very odd to me that essentially the same code can work on one site but not another. I hope you have some clue as to what is happening.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @awol

    I pinged our SLS Team to review this and see what will be possible in this case. Please keep in mind that our SLS Team deals with more complicated issues, so it may take a little longer for them to reply here. Thank you for your patience while we look into this further.

    Kind Regards,
    Kris

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @awol,

    I hope you’re doing well today!

    Please try replacing the non-working code with the following snippet:

    <?php
    add_filter( 'forminator_cform_render_fields', function ( $wrappers, $model_id ) {
        // Form id where this code will be applied
        if ( $model_id != 2280 && $model_id != 2300 ) {
            return $wrappers;
        }
    
        $user_id = get_current_user_id();
        $members = friends_get_friend_user_ids( $user_id );
        if ( ! empty( $members ) ) {
            $new_options = array();
            $names = array();
            foreach( $members as $key => $users_id ) {
                $member = get_user_by('id', $users_id);
                $new_options[] = array(
                    'label' => $member->user_login,
                    'value' => $member->user_login,
                    'limit' => '',
                    'key'   => forminator_unique_key(),
                );
            }
            $names['options'] = $new_options;
        }
    
        // Define select fields to be populated
        $select_fields_data = array(
            'select-1' => 'users',
        );
    
        // Look for individual fields or fields in groups
            foreach ( $wrappers as $wrapper_key => $wrapper ) {
                if ( ! isset( $wrapper[ 'fields' ] ) ) {
                    continue;
                }
                for ($index = 0; $index < count($wrapper['fields']); $index++) {
                    if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && $select_fields_data[ $wrapper[ 'fields' ][ $index][ 'element_id' ] ] === 'users'){
                        $select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][$index]['element_id'], true );
                        if ( $select_field ) {
                            Forminator_API::update_form_field( $model_id, $wrapper['fields'][$index]['element_id'], $names );
                            $wrappers[ $wrapper_key ][ 'fields' ][ $index ][ 'options' ] = $new_options;
                        }
                    }
                }
            }
        return $wrappers;
    }, 10, 2);

    Let us know if there’s still any issue!

    Best Regards,
    Dmytro

    Thread Starter AWOL

    (@awol)

    Hi Dmytro @wpmudevsupport16,

    Thanks and I can confirm that works, although I am a little puzzled why the other one works on one site but not the other? If you have time to explain it I would be interested to know for future reference. Obviously I understand if you are too busy.

    Hi @awol,

    Hope this message finds you well and we are glad to hear is working.

    I pinged our Developers about this, once we get a reply from them, we will let you know.

    Best regards,
    Laura

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @awol

    I hope you are doing well.

    Just an update from our second line support, unfortunately without some deeper check we can’t really say why it is working in one site but not on another one, however, I suggest keeping the working code and you should be fine on that site as well.

    Best Regards
    Patrick Freitas

    Thread Starter AWOL

    (@awol)

    Hi Patrick @wpmudevsupport12,

    Thanks, I will do that. Another of coding’s little mysteries I guess.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Dropdown list of users (again)’ is closed to new replies.