Usage of MB Group and custom Field

Support MB Group Usage of MB Group and custom Field

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5556
    christophe.bchristophe.b
    Participant

    Hello,
    i need to let a user type a long url in a field and then upon saving using a service to short url and provide tracking. I was trying to use the filter rwmb_{$field_id}_value but they are not working for me when using groups. So i went ahead and create a custom field in the following way :

    
    class RWMB_Partner_Field extends RWMB_Text_Field {
    
    	/**
    	 * Get meta values to save.
    	 * Save terms in custom field in form of comma-separated IDs, no more by setting post terms.
    	 *
    	 * @param mixed $new     The submitted meta value.
    	 * @param mixed $old     The existing meta value.
    	 * @param int   $post_id The post ID.
    	 * @param array $field   The field parameters.
    	 *
    	 * @return string
    	 */
    	public static function value( $new, $old, $post_id, $field ) {
    		error_log('new value to parse : '.$new);
    		$calc = self::shortUrl($new);
    		return $calc;
    	}
    }
    

    when used outside of a group this function get called without any issue. But when using it in a group (actually 2 level of nesting) this never get called.
    I notice that also if i define a save method it doesn't get called either, while a get_attributes will get called.

    here is my structure used :

    
    add_filter( 'rwmb_meta_boxes', 'sa_release_mb' );
    
    function sa_release_mb(){
    	$prefix = 'sa';
    	$mb[] = array(
    
    		'id'         => 'release',
    		'title'      => __( 'Release Infos', 'sa_mb' ),
    		'post_types' => array( 'post'),
    		'context'    => 'normal',
    		'priority'   => 'high',
    		'fields' => array(
    			array(
    				'name'  => __( 'Release', 'sa_mb' ),
    				'id'    => $prefix . 'release',
    				'type'  => 'group',
    				'clone' => true,
    				'sort_clone'  => true,
    				'collapsible' => true,
    				'fields'=> array(
    					array(
    						'name'  => 'Model',
    						'id'    => $prefix.'model',
    						'type'  => 'text',
    						'size'  => 60,
    						'columns'=> 12
    
    					),
    					array(
    						'name'  => 'Colorway',
    						'id'    => $prefix.'cw',
    						'type'  => 'text',
    						'size'  => 60,
    						'columns'=> 12
    					),
    					array(
    						'name'  => 'SKU',
    						'id'    => $prefix.'sku',
    						'type'  => 'text',
    						'columns'=> 12
    					),
    					array(
    						'name'  => __( 'Main Retailer', 'sa_mb' ),
    						'id'    => $prefix . 'main-retailer',
    						'type'  => 'group',
    						'clone' => false,
    						'collapsible' => true,
    						'group_title'   => 'Main Retailer',
    						'fields'=> array(
    							array(
    								'name'  => 'Vendor',
    								'id'    => $prefix.'main-vendor',
    								'type'  => 'post',
    								'post_type' => 'partner',
    								'field_type'=> 'select_advanced',
    								'columns'   => 6
    							),
    							array(
    								'name'  => 'Link',
    								'id'    => $prefix.'main-link',
    								'type'  => 'partner',
    								'columns'=> 6,
    								'size'  => 45
    
    							),
    							array(
    								'name'  => 'Price',
    								'id'    => $prefix.'main-price',
    								'type'  => 'number',
    								'columns'   => 4
    							),
    							array(
    								'name'  => 'Currency',
    								'id'    => $prefix.'main-currency',
    								'type'  => 'select',
    								'placeholder'   => 'Choose Currency',
    								'options'   => array(
    									'EUR'   => '€',
    									'DOL'   => '$',
    									'UKP'   => '£'
    								),
    								'columns'   => 8
    							),
    							array(
    								'name'  => 'Date',
    								'id'    => $prefix.'main-date',
    								'type'  => 'date',
    								'columns'   => 12
    							),
    							array(
    								'name'  => 'Time',
    								'id'    => $prefix.'main-time',
    								'type'  => 'time',
    								'js_options' => array(
    									'timeFormat'    => 'HH:mm z',
    									'timezoneList'  => array(
    										array('value' => '+0100', 'label' => 'CET'),
    										array('value' => '+0000', 'label' => 'GMT')
    									)
    								),
    								'columns'   => 12
    							)
    						)
    					),
    
    					array(
    						'name'  => __( 'Other Retailers', 'sa_mb' ),
    						'id'    => $prefix . 'other-retailer',
    						'type'  => 'group',
    						'clone' => true,
    						'collapsible' => true,
    						//'group_title'   => array('field' => $prefix.'vendor'),
    						'fields'=> array(
    							array(
    								'name'  => 'Vendor',
    								'id'    => $prefix.'vendor',
    								'type'  => 'post',
    								'post_type' => 'partner',
    								'field_type'=> 'select_advanced',
    								'columns'   => 6
    							),
    							array(
    								'name'  => 'Link',
    								'id'    => $prefix.'link',
    								'type'  => 'partner',
    								'columns'=> 6,
    								'size'  => 45
    
    							),
    							array(
    								'name'  => 'Price',
    								'id'    => $prefix.'price',
    								'type'  => 'number',
    								'columns'   => 4
    							),
    							array(
    								'name'  => 'Currency',
    								'id'    => $prefix.'currency',
    								'type'  => 'select',
    								'placeholder'   => 'Choose Currency',
    								'options'   => array(
    									'EUR'   => '€',
    									'DOL'   => '$',
    									'UKP'   => '£'
    								),
    								'columns'   => 8
    							),
    							array(
    								'name'  => 'Date',
    								'id'    => $prefix.'date',
    								'type'  => 'date',
    								'columns'   => 12
    							),
    							array(
    								'name'  => 'Time',
    								'id'    => $prefix.'time',
    								'type'  => 'time',
    								'js_options' => array(
    									'timeFormat'    => 'HH:mm z',
    									'timezoneList'  => array(
    										array('value' => '+0100', 'label' => 'CET'),
    										array('value' => '+0000', 'label' => 'GMT')
    									)
    								),
    								'columns'   => 12
    							)
    						)
    					)
    				)
    			)
    		)
    	);
    	return $mb;
    }
    

    Hoping to receive some help or a workarround as i need to get in production this code pretty soon and i'm running out of option.

    #5568
    Anh TranAnh Tran
    Keymaster

    Hi,

    The sub-fields of groups are not passed through the value method of the field at the moment (as well as filters). They're treated as a part of the group value. And thus, you should filter the value of the group when the data is submitted using the this filter:

    https://metabox.io/docs/filters/#section-rwmb_field_id_value

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Usage of MB Group and custom Field’ is closed to new replies.