How to target a specific index of cloned fields with Metabox Group

Support MB Group How to target a specific index of cloned fields with Metabox Group

  • This topic has 3 replies, 2 voices, and was last updated 1 year ago by PeterPeter.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #45099
    CodogCodog
    Participant

    Hi there,
    I have a frontend submission form that allows users to "Clone" a group of fields (up to X3). The cloned groups of fields are saved in the database as serialized data. All works fine.

    The Problem
    ============
    I have created a callback function that successfully passes some data to a hidden field within the cloned groups (for another purpose). However, this data is logically captured (stored) within "ALL cloned groups"? How do I "target a specific clone" created by a user via PHP in my function? I was assuming by it's index in the database, but I cannot seem to get the syntax right for the job?

    Example:

    // Clone #0
    [0] => Array
    (
        [hidden_field] => null
        [another_field] => data
        [another_field] => data
        etc....
    )
    // Clone #1
    [1] => Array // How would I target this clone ONLY via a PHP conditional check?
    (
        [hidden_field] => callback function data
        [another_field] => data
        [another_field] => data
        etc....
    )
    
    // Clone #2
    [1] => Array 
    (
        [hidden_field] => null
        [another_field] => data
        [another_field] => data
        etc....
    )

    Any help appreciated 🙂

    #45112
    PeterPeter
    Moderator

    Hello,

    You can create a loop to iterate through the cloneable group value and check the key to update the hidden field of a specific clone group. For example:

    $group_values = rwmb_meta( 'group_id' ) ?: [];
    foreach ( $group_values as $group_key => $group_value ) {
    	if ( $group_key == 1 ) {
    		$group_value[1]['hidden_field'] = 'something';
    	}
    }

    Following the documentation https://docs.metabox.io/extensions/meta-box-group/#sub-field-values

    #45131
    CodogCodog
    Participant

    Hi Peter,
    thanks for your guidance 🙂 Unfortunately your suggested method is not working for me right now? I tried a very simple implementation to see if I can target and output a simple message and update the hidden_field value within an individual clone.

    My test function:
    ==================

    function output_to_clone(){
    
    	$post_id = $_GET['rwmb_frontend_field_post_id'];
    
    	$group_values = get_post_meta($post_id, 'group_id', true); 
    
    	$msg = '';
    
    	foreach ( $group_values as $group_key => $group_value ) {
    		if ( $group_key == 1 ) {
    			$group_value[1]['hidden_field'] = 'Something New';
    			$msg .= '<div class="alert alert-danger" role="alert">Group Key = '.$group_key.'</div>';
    		} else {
    			$msg .= '';
    		}
    	}
    	return $msg;
    }

    Function output
    ================

    Then within the cloneable group fields I just added the function call:

                [
                    'id'                => 'group_id',
    				'add_button'        => __( 'Add more', 'text-domain' ),
                    'type'              => 'group', 
                    'clone'             => true,     
                    'sort_clone'        => true,     
    				'group_title'       => 'Title {#}',
    				'collapsible'       => true,
    				'save_state'        => true, 
    				'max_clone'         => 3,
    
                    'fields'     => [
    					[
    						'id'   => 'hidden_field',
    						'type' => 'hidden',
    						'std'  => 'Hidden Value',
    					],
                        [
    						'before'           =>   output_to_clone(), // Function output
                            'name'             => __( 'Field One', 'text-domain' ),
    						'desc'             => __( 'Add a unique name.', 'text-domain' ),
                            'id'               => 'field_one',
    						'type'             => 'text',
                        ],
                        [
                            'name'             => __( 'Field Two', 'text-domain' ),
    						'desc'             => __( 'Enter a unique reference.', 'text-domain' ),
                            'id'               => 'field_two',
                            'type'             => 'text',
    						'required'       => true,
                        ],
                    ],
                ],

    Expected Test Outcome
    =======================

    1) The $group_key == 1 conditional check would output the $msg value for the individual clone that matches the key. All other clones would return an empty $msg.
    2) The $group_key == 1 - hidden_field would be updated to "Something New".

    Actual Test Outcome
    =======================

    1) The $group_key == 1 conditional check outputs the same $msg value for ALL cloned fields (IE: Group Key = 1).
    2) The $group_key == 1 - hidden_field value is NOT updated.

    I am obviously misunderstanding something here? Can you help me understand where I am going wrong? Thanks in advance 🙂

    #45151
    PeterPeter
    Moderator

    Hello,

    What is the action/filter that you hook the callback function output_to_clone to? If you want to update the group field value, you can use the function update_post_meta()
    Refer to this topic https://support.metabox.io/topic/add-cloneable-group-using-update_post_meta-whilst-preserving-existing-data/

    If you cannot complete the task, we offer a customization service with an extra fee. Please submit this form with your details, our development team will get back to you soon.
    https://metabox.io/contact/

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