BuddyDev

Search

[Resolved] conditionsprofile field (if i am a women, i search man)

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21228

    hello
    Can you help me?

    I wish that in my profile fields to put if I am a woman I must look for a man.
    If I am a man I am looking for a woman.

    It will be necessary that if I select that I am a woman the field (I search a man is put automatically) and vice versa.

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #21232

    Hi,
    Welcome to BuddyDev.

    If I understand correctly, you are looking to restrict the options of a profile field? This is not doable(we can certainly do it via hooks but let us see if we really need to do).

    I am assuming your purpose is to either display it on profile or use it for search purpose?
    If it is for search purpose, It is better to decide the value based on the reverse of “I am a” field.

    If it is for display purpose only, why not create two fields(not advisable for search) and show hide using conditional field?

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21233

    hello thanks
    i have Conditional Profile Fields for BuddyPress plugin
    But ot arrived to have that i want
    Ca you help me?

    My field are

    I am
    A woman
    A man

    I research
    A woman
    A man

    So if I select that I am a woman it is necessary that the man is put automatically and vice versa

    https://hobjectifhallal.com/membres/admin/

    thanks

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21379

    hello
    Nothing can help me?

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #21400

    Hi,
    I am sorry but the way you are looking for is not available in BuddyPress.

    What you are looking for a derived/caculated field from the values of another field.

    The general strategy in this case is to use only one field and calculate the other value.

    For example, If a person is Man, you can write code to derive the Research field value as woman.

    There does not seem to be any existing solution for this kind of use case. I do see that you can write some code to sync value of another field based on a field.

    If you can post me the field ids for both the field(and exact options), I will be able to provide you some code to resolve it.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21401

    hello i can give all my access.
    If you want.

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21402

    if a man, i search (Required) a woman
    if a woman (required) a man

    I not arrived to put this fonction
    can you check for me please?

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #21406

    Hi,
    I had a look at the registration page.

    1. Please disable the conditional field plugin. You do not need that for this case.
    2. I will post some code(in couple of hours) to help you get it working.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 34
    bootz on #21408

    i disable conditionnal plugin

    Thanks for your help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #21418

    Hi,

    Please put this code in your bp-custom.php (or theme’s functions.php)
    It will hide the I Research field on Registration/Edit profile and update its value from the “I am” field.

    I have used the field id from your site.

    
    
    /**
     * Update value of the dest field when source is updated.
     *
     * @param BP_XProfile_ProfileData $object data object.
     */
    function buddydev_sync_to_field_data_to_other_field_on_update( $object ) {
    	// Field id, whose value we use to determine the calculated value for the synchronized field.
    	$sync_source_field_id      = 3; // field when change, triggers change to the sync field.
    	// this is the field whose value will be updated based on the calculation.
    	$sync_dest_field_id = 6;// field we will update.
    
    	// If it is not the source field id, don't do anything.
    	if ( $object->field_id != $sync_source_field_id ) {
    		return;
    	}
    
    	$value       = $object->value;
    	$value       = maybe_unserialize( $value );
    	$other_value = '';
    
    	if ( empty( $value ) ) {
    		// delete.
    		xprofile_delete_field_data( $sync_dest_field_id, $object->user_id );
    
    		return;
    	}
    	// Determine, what value we should set.
    	// This is the block that we use to calculate
    	// you will need to update for your use case.
    	if ( 'Un Homme' == $value ) {
    		$other_value = 'Une Femme';
    	} else {
    		$other_value = 'Un Homme';
    	}
    	// If the sync_destination field is select/radio, make sure the calculated value is what you have
    	// added as option the the field add/edit screen. Should match exactly.
    	// set teh value.
    	xprofile_set_field_data( $sync_dest_field_id, $object->user_id, $other_value );
    }
    // On update.
    add_action( 'xprofile_data_after_save', 'buddydev_sync_to_field_data_to_other_field_on_update' );
    
    // On delete.
    function buddydev_sync_to_field_data_to_other_field_on_delete( $object ) {
    // Field id, whose value we use to determine the calculated value for the synchronized field.
    	$sync_source_field_id      = 3; // field when change, triggers change to the sync field.
    	// this is the field whose value will be updated based on the calculation.
    	$sync_dest_field_id = 6;// field we will update.
    
    	// If it is not the source field id, don't do anything.
    	if ( $object->field_id != $sync_source_field_id ) {
    		return;
    	}
    
    	xprofile_delete_field_data( $sync_dest_field_id, $object->user_id );
    }
    add_action( 'xprofile_data_after_delete', 'buddydev_sync_to_field_data_to_other_field_on_delete' );
    
    /**
     * Hide the synchronized field from Registration/Edit profile fields. Its value is set programatically.
     * @param $args
     *
     * @return mixed
     */
    function buddydev_exclude_sync_profile_field_in_edit_context( $args ) {
    
    	if ( ! bp_is_user_profile_edit() && ! bp_is_register_page() ) {
    		return $args;
    	}
    
    	$sync_to_field    = 6;
    	$exclude_fields   = isset( $args['exclude_fields'] ) ? explode( ',', $args['exclude_fields'] ) : array();
    	$exclude_fields[] = $sync_to_field;
    
    	$args['exclude_fields'] = join( ',', $exclude_fields );
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_profile_parse_args', 'buddydev_exclude_sync_profile_field_in_edit_context' );
    
    

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved