Support

Account

Home Forums ACF PRO Developing new field types: two questions related to the field structure

Unread

Developing new field types: two questions related to the field structure

    • foyle

    • November 5, 2020 at 12:35 am

    I’m working on several new field types: fields that would allow me to easilly define properties such as padding, margin, text align for each CSS breakpoint of my framework. For context, you can download a simplified version of said field pruned of any superfluous feature here: https://github.com/foyle82/acf-test-conditional . Inside of the repository you can also check the file screen.jpg to better understand/see the purpose of the plugin.

    Now i have two very specific questions:

    
    function render_field( $field ) {
    
        acf_render_field_wrap( [
    
            'type'              => 'group',
            'name'              => $this->field['name'],
            'value'             => $this->field['value'],
            'label'             => __( 'Group', 'acf' ),
    
            'sub_fields'     => []
    
        ] );
    
    }
    

    Question #1: Group field as wrapper

    For context see: https://github.com/foyle82/acf-test-conditional/blob/main/fields/field.php#L24-L31

    I’m going to rely on some conditional logic in order to show/hide fields. I found out that using acf_render_field_wrap() (while inside of render_field()), defining a group and populating the subfields as I please, does seem to work properly. Since I found no documentation that either describes or discourages this solution I would like to know if this is a correct approach or if it may lead to issues down the line.

    
    function render_field( $field ) {
    
        acf_render_field_wrap( [
    
            'type'              => 'group',
            'name'              => $this->field['name'],
            'value'             => $this->field['value'],
            'label'             => __( 'Group', 'acf' ),
    
            'sub_fields'     => [
    
                [
    
                    'label'                     => 'Select',
                    'name'                      => 'select',
                    'key'                       => 'select',
                    'type'                      => 'select',
    
                    'choices'                   => [],
    
                ]
    
            ]
    
        ] );
    
    }
    

    Question #2: Sub field keys

    For context see: https://github.com/foyle82/acf-test-conditional/blob/main/fields/field.php#L33-L47

    Each subfield of the above group, currently uses the same “name” and “key” keys (note that this does not apply to the parent group field, just to the inner subfields). Everything works properly and proves easier to handle (expecially when it comes to generating fields on the fly depending on the number/name of breakpoints supported by the current theme). Hence the question is: can this approach lead to future issues or is it well supported and safe to use?

    Thanks in advance.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.