Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Angelo Rocha

    (@angelorocha)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The fact that you’re getting Array in your serialized data a:2:{i:0;s:5:"Array";i:1;s:5:"Array";} has me wondering if somehow you need to do a bit more processing somehow so that it actually serializes the arrays instead of just inserting in the fact that it is one.

    I know you pointed out the snippets wiki, but can you also provide the exact code you have saved to an actual file inside your WP install? It’ll be much more efficient to copy/paste that into our local installs for testing/debugging.

    Thread Starter Angelo Rocha

    (@angelorocha)

    My custom field code:

    function cmb2_field_type_options( $value = false ) {
    	$type_list   = array(
    		'text'  => 'Texto',
    		'mail'  => 'Email',
    		'money' => 'Dinheiro',
    		'date'  => 'Data',
    	);
    	$field_types = '';
    	foreach ( $type_list as $key => $type ):
    		$field_types .= "<option value='$key' " . selected( $value, $key, false ) . ">$type</option>";
    	endforeach;
    
    	return $field_types;
    }
    
    add_filter( 'cmb2_render_formfield', 'cmb2_render_formfield_callback', 10, 5 );
    function cmb2_render_formfield_callback( $field, $value, $object_id, $object_type, $field_type ) {
    	$value = wp_parse_args(
    		$value = array(
    			'field_id'    => '',
    			'field_label' => '',
    			'field_type'  => '',
    			'field_size'  => '',
    		)
    	);
    	?>
        <table>
            <tr>
                <td>
                    <label for="<?php echo $field_type->_id( '_field_id' ) ?>">ID do Campo</label>
    				<?php
    				echo $field_type->input( array(
    					'name'  => $field_type->_name( '[field_id]' ),
    					'id'    => $field_type->_id( '_field_id' ),
    					'value' => $value['field_id'],
    					'desc'  => ''
    				) )
    				?>
                </td>
                <td>
                    <label for="<?php echo $field_type->_id( '_field_label' ) ?>">Título do campo</label>
    				<?php
    				echo $field_type->input( array(
    					'name'  => $field_type->_name( '[field_label]' ),
    					'id'    => $field_type->_id( '_field_label' ),
    					'value' => $value['field_label'],
    					'desc'  => ''
    				) )
    				?>
                </td>
                <td>
                    <label for="<?php echo $field_type->_id( '_field_type' ) ?>">Tipo do campo</label>
    				<?php
    				echo $field_type->select( array(
    					'name'    => $field_type->_name( '[field_type]' ),
    					'id'      => $field_type->_id( '_field_type' ),
    					'options' => cmb2_field_type_options( $value['field_type'] ),
    					'desc'    => ''
    				) )
    				?>
                </td>
                <td>
                    <label for="<?php echo $field_type->_id( '_field_size' ) ?>">Tamanho do campo</label>
    				<?php
    				echo $field_type->input( array(
    					'name'  => $field_type->_name( '[field_size]' ),
    					'id'    => $field_type->_id( '_field_size' ),
    					'value' => $value['field_size'],
    					'desc'  => ''
    				) )
    				?>
                </td>
            </tr>
        </table>
    	<?php
    }

    My call:

    	$form->add_field( array(
    		'name'       => 'Campos',
    		'desc'       => 'Adiciona campos ao formulário',
    		'id'         => '_form_fields',
    		'type'       => 'formfield',
    		'repeatable' => true,
    		'text'       => array(
    			'add_row_text' => 'Adicionar Campo',
    		),
    	) );

    Thanks Michael

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Gonna have to see if I can find some extra help to help me understand things as a whole here, as I’m not the most knowledgable in creating my own field types (something I do have interest in changing).

    That said, at least on the saving front, I believe you’re going to for sure want to create your own sanitization callback for your field. I did something like the following:

    function cmb2_sanitize_formfield_callback( $override_value, $values, $object_id, $field_args, $sanitizer_object ) {
    	$new = [];
    
    	if ( is_array( $values ) ) {
    		foreach( $values as $value ) {
    			$new[] = array_map( 'sanitize_text_field', $value );
    		}
    	}
    
    	return $new;
    }
    add_filter( 'cmb2_sanitize_formfield', 'cmb2_sanitize_formfield_callback', 10, 5 );
    

    and it saved my meta field as such:

    a:3:{i:0;a:4:{s:8:"field_id";s:0:"";s:11:"field_label";s:0:"";s:10:"field_type";s:4:"text";s:10:"field_size";s:0:"";}i:1;a:4:{s:8:"field_id";s:0:"";s:11:"field_label";s:0:"";s:10:"field_type";s:4:"text";s:10:"field_size";s:0:"";}i:2;a:4:{s:8:"field_id";s:0:"";s:11:"field_label";s:0:"";s:10:"field_type";s:4:"text";s:10:"field_size";s:0:"";}}

    The default callbacks for sanitization are calling sanitize_text_field on your nested arrays and causing it to save “Array”.

    However, where I’m scratching my own head is the rendering portion, as that is still having an “Array” value or empty meta passed in instead of actual array data.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Hey Angelo, you were pretty close, but there were a few issues with your code causing things not to work, including the fact that whenever you create custom fields that work with repeatable, you have to do some sanitization/escaping magic (e.g. the comment on the issue you linked to: https://github.com/CMB2/CMB2/issues/116#issuecomment-66845137)

    I have added your custom field to the snippet repo so you can grab it/reuse it, but it should provide a good map as to how this should work for you: https://github.com/CMB2/CMB2-Snippet-Library/blob/master/custom-field-types/form-field-field-type.php

    Thread Starter Angelo Rocha

    (@angelorocha)

    Very thanks Michael, sorry for the delay.

    Justin, thanks for this tip.

    Problem solved.

    Thread Starter Angelo Rocha

    (@angelorocha)

    I try add a “checkbox” field in this code, but the after save data, the value don’t change.
    Any idea?

    My code, line 96: https://pastebin.com/jWV3EBZK

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Is this for a custom field type that you’ve worked on?

    Are you getting the values expected inside the filter callbacks you’re creating for sanitizing and saving? Are the values being saved in the database regardless of how it’s coming out when it’s time to display things? That last one would answer if it’s an input or output issue.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom field type vs Repeatable attr’ is closed to new replies.