BuddyDev

Search

[Resolved] List of user friends as options in dropdown box

  • Participant
    Level: Initiated
    Posts: 18
    Nitin Shah on #43325

    On a site I am working on, I am using Formidable forms and BuddyPress together. In the formidable forms, there is a field type user id that displays a list of all registered users in a dropdown. I would like to filter the list of users to only include the buddypress friends of the user filling the form. I thought I can use friends_get_friend_user_ids( bp_loggedin_user_id() ); to filter the list but I am not sure I am doing it correctly.

    The code below works perfectly to filter the users in the dropdown to those who have the user role “author”. I need help to modify this to filter so that user friends are listed instead of authors.

    add_filter('frm_setup_new_fields_vars', 'show_user_dropdown', 15, 2);
    add_filter('frm_setup_edit_fields_vars', 'show_user_dropdown', 15, 3);
    function show_user_dropdown($values, $field, $entry_id=false){
    if( $field->id == 898 ){
    $values['type'] = 'select';
    $values['use_key'] = true;
    $values['custom_html'] = FrmFieldsHelper::get_default_html('select');
    $wp_user_search = new WP_User_Query( array( 'role' => 'author', 'fields' => array('user_login', 'ID') ) );
    $authors = $wp_user_search->get_results();
    $values['options'] = array();
    foreach ($authors as $a) {
    $values['options'][$a->ID] = $a->user_login; } }
    return $values; }

    The buddypress function that I should use for getting a list of friends of users I think is: friends_get_friend_user_ids( bp_loggedin_user_id() );

    So I tried by modifying the about code in the following manner:

    add_filter('frm_setup_new_fields_vars', 'show_user_dropdown', 15, 2);
    add_filter('frm_setup_edit_fields_vars', 'show_user_dropdown', 15, 3);
    function show_user_dropdown($values, $field, $entry_id=false){
    if( $field->id == 173 ){
    $values['type'] = 'select';
    $values['use_key'] = true;
    $values['custom_html'] = FrmFieldsHelper::get_default_html('select');
    $friend_ids = friends_get_friend_user_ids(1);
    $values['options'] = array();
    foreach ($friend_ids as $a) {
    $values['options'][$a->ID] = $a->ID; } }
    return $values; }

  • Participant
    Level: Initiated
    Posts: 18
    Nitin Shah on #43326

    I am not able to edit the above post, so posting a slight correction here, the $field->id == 173 for both the codes. That represents the field id of the user field in formidable.

  • Keymaster
    (BuddyDev Team)
    Posts: 21486
    Brajesh Singh on #43331

    Hi Nitin,
    You are using correct function for fetching friends. The “friends_get_friend_user_ids” will give an array.

    We lack experience with Formidable Form. I will suggest posting on their forum to get assistance.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 18
    Nitin Shah on #43337

    Thank you for confirming that the function I am using is correct. I have requested help on the formidable support as well and am waiting for a response from them. Meanwhile just to know my understanding of how the functions and array work, does this much part of the code look right to you?

    $friend_ids = friends_get_friend_user_ids( bp_loggedin_user_id() );
    $values['options'] = array();
    foreach ($friend_ids as $a) {
    $values['options'][$a->ID] = $a->ID; } }
    return $values;

  • Participant
    Level: Initiated
    Posts: 18
    Nitin Shah on #43338

    I have got this to work. Here is the code for anyone else who may come looking for it:

    add_filter('frm_setup_new_fields_vars', 'show_user_dropdown', 15, 2);
    add_filter('frm_setup_edit_fields_vars', 'show_user_dropdown', 15, 3);
    function show_user_dropdown($values, $field, $entry_id=false){

    if( $field->id == 173 ){ // replace 173 with the field id for the user id field in the form
    $values['type'] = 'select';
    $values['use_key'] = true;
    $values['custom_html'] = FrmFieldsHelper::get_default_html('select');
    $user_id = get_current_user_id();
    $friend_ids = friends_get_friend_user_ids( $user_id );
    $values['options'] = array();
    foreach ($friend_ids as $friend_id) {
    $friend = get_user_by('id', $friend_id);
    $values['options'][$friend->ID] = $friend->user_login; } }
    return $values; }

  • Keymaster
    (BuddyDev Team)
    Posts: 21486
    Brajesh Singh on #43342

    Hi Nitin,

    Thank you for sharing. It’s good to know that you were able to resolve it.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved