Fix for nesting groups

Support MB Group Fix for nesting groups

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2550
    aconwayaconway
    Participant

    Hi, I saw you mentioned in another support question you were working on a fix for nesting clonable groups, I needed nesting non-cloneable groups for a project so I made some simple changes to the RWMB_Group_Field class to enable them.

    The main issue was the current class using and rewriting a single static meta value for all groups. Instead I'm setting a field parent_meta on the child field attributes in the loop in the html function:

    $child_field['attributes']['name'] = $child_field['field_name'] = self::child_field_name( $field['field_name'], $child_field['field_name'] ); 
    if(isset($meta[$child_field['id']])){
    		$child_field['attributes']['parent_meta'] = $meta[$child_field['id']];
    }

    then looking to that in the child_field_meta function:

    
    if ( isset( $child_field['attributes']['parent_meta'] ) )
    	{
    	$meta = $child_field['attributes']['parent_meta'];
    
    }

    Not sure if you've already figured out this or made a similar fix, I'm happy to share my code with the hopes of it being integrated into a future release version.

    #2560
    Anh TranAnh Tran
    Keymaster

    Thanks a lot, aconway! The reason it's hard to deal with is cloning. But it's still good if we have a solution for none-cloning groups. I will check your code and let you know.

    #2569
    aconwayaconway
    Participant

    Hi,

    what I posted above isn't exactly right, I ran into the issue that the attributes are set as strings on the metabox html object so you have an issue if there's a array (ie an image value or something).

    I made it instead like this

    	static function html( $meta, $field )
    	{
    		// Get parent field and filter to child field meta value
    		if(!isset($field['parent_meta'])){
    			add_filter( 'rwmb_field_meta', array( __CLASS__, 'child_field_meta' ), 10, 3 );
    		}
    
    		ob_start();
    
    		foreach ( $field['fields'] as $child_field )
    		{
    			$child_field['attributes']['name'] = $child_field['field_name'] = self::child_field_name( $field['field_name'], $child_field['field_name'] ); 
    			if(isset($meta[$child_field['id']])){
    				$child_field['parent_meta'] = $meta[$child_field['id']];
    			}
    			call_user_func( array( RW_Meta_Box::get_class_name( $child_field ), 'show' ), $child_field, RWMB_Group::$saved);	
    		}
    
    		// Remove filter to child field meta value and reset class's parent field's meta
    		if(!isset($field['parent_meta'])){
    			remove_filter( 'rwmb_field_meta', array( __CLASS__, 'child_field_meta' ) );
    		}
    
    	   return ob_get_clean();
    	}
    static function child_field_meta( $meta, $child_field, $saved )
    	{
    
    		$meta = '';
    		$id   = $child_field['id'];
    
    		if ( isset( $child_field['parent_meta'] ) )
    		{
    			$meta = $child_field['parent_meta'];
    		}
    		elseif ( ! $saved && isset( $child_field['std'] ) )
    		{
    			$meta = $child_field['std'];
    		}
    		elseif ( $child_field['multiple'] )
    		{
    			$meta = array();
    		}
    
    		return $meta;
    	}
    
    #2574
    Anh TranAnh Tran
    Keymaster

    Hi aconway,

    Thanks again for your contribution. I've looked at your code and it works like a charm.

    The main thing here is "capture" the parent value of child field. You did that by assigning parent value to "parent_meta" key of the child field and get it back in the child_field_meta function. Althogh this works perfectly, this leads to a problem of redundant info as the parent value of 1 group is stored in ALL its children.

    So, I modified it a little by using a queue. The queue stores parent value. I use push/pop to add/retrieve parent value from the queue. Just 1 single variable to store all parent values. I think it's a little better in memory usage.

    The new version 1.0.7 is just released with this. Hope you enjoy it.

    Again, thanks a lot for your contribution!

    #2599
    aconwayaconway
    Participant

    Great, thanks! That was a good catch to make it more efficient.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Fix for nesting groups’ is closed to new replies.