Support

Account

Home Forums Feature Requests Repeater: default fields/data

Solved

Repeater: default fields/data

  • I use the repeater field quite a lot. It’s great that we can specify default data, but it will also be great if we could setup default fields.

    For example; I create a repeated field with a dropdown box “title” and an image “attachment”. This repeater field is setup to be shown if the post type = page.

    Every time you click ‘add new page’ the repeater field is displayed and the user can add as many rows as they like. Great.

    But, I would like the repeater field to be already setup by default with X amount of rows already in place with something selected in the dropdown box.

    I hope this makes sense.

    • Elliot

    • July 8, 2013 at 12:51 pm

    Hi @steve Holland

    Thanks for the request. This isn’t as easy as you may think to add in.

    Would the ‘x’ number of rows contain different default values in the sub fields?

  • Thanks Elliot.

    Yes, this would be quite an advanced feature, but something to think about.

    Yes the subfields have different values.

    I have managed to find a solution, I created a page with the repeater sub-fields laid out. I saved that page without a title and then use a plugin to duplicate it whenever I need a new page with those same repeater subfields already there.

    I just think this would be a good feature as sometimes you have a lot of repeater subfields (like dropdown boxes with set values) that you use almost every time. A feature like this would ensure that they’re already setup when you create a new post.

    • Elliot

    • July 8, 2013 at 3:31 pm

    Hi @steve Holland

    Totally agree. If I have the time, I would love to add this in.

    But for now, it sounds like you have a solid work around. Good work!

  • I believe I’ve found an actually really simple solution for this – set $value to an array of your default values inside the acf/load_value hook.

    add_filter('acf/load_value/key=field_54aa5b3b008b0',  'afc_load_my_repeater_value', 10, 3);
    function afc_load_my_repeater_value($value, $post_id, $field) {
    	if ($post_id === false) {
    		$value	= array();
    		$value[] = array(
    			field_54aa5beb008b1 => 'Hours',
    			field_54aa5c25008b2 => 1
    		);
    		$value[] = array(
    			field_54aa5beb008b1 => 'Minutes'
    		);
    		$value[] = array(
    			field_54aa5beb008b1 => 'Seconds'
    		);
    	}
    	return $value;
    }
    

    I have more details on this post.

  • This line $post_id === false does not work anymore, one must do it like this: get_post_status($post_id) === 'auto-draft'

  • Any chance this has been implemented and I’m just missing where? I’ve needed this functionality a few times now (a repeater field with 3 or 4 default items, ie: # of Bedrooms, # of Bathrooms, Sq.Ft) and then more fields are required but not always the same ones (ie: Yard Size, Patio Size, Strata Fees).

    All field types would be the same (simple text fields).

  • A clean approach here tested in version 5.5.7, you can just set a default value in the repeater field like this (note: you need to do these edits in the PHP export code. Note 2: Check the ‘default_value’ key passed to the array):

    
    array (
        'key' => 'key_58c7ee3a37aee',
        'label' => 'Table',
        'name' => 'table',
        'type' => 'repeater',
        'instructions' => '',
        'required' => 1,
        'conditional_logic' => 0,
        'wrapper' => array (
            'width' => '',
            'class' => '',
            'id' => '',
        ),
        'collapsed' => '',
        'min' => 0,
        'max' => 0,
        'default_value' => array(
            array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
            ),
            array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
            ),
        ),
        'layout' => 'table',
        'button_label' => 'Add Row',
        'sub_fields' => array (
            array (
                'key' => 'field_58984ddb5b8cc',
                'label' => 'Title',
                'name' => 'title',
                'type' => 'text',
                'instructions' => '',
                'required' => 1,
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '40%',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'maxlength' => '',
                'readonly' => 0,
                'disabled' => 0,
            ),
            array (
                'key' => 'field_58985045abac0',
                'label' => 'Content',
                'name' => 'content',
                'type' => 'text',
                'instructions' => '',
                'required' => 1,
                'conditional_logic' => 0,
                'wrapper' => array (
                    'width' => '60%',
                    'class' => '',
                    'id' => '',
                ),
                'default_value' => '',
            ),
        ),
    ),
    

    Hope this saves someone’s time,
    Cheers !

  • Bloody hell, my solution is no longer working in 5.5.13 … I don’t understand why they would remove this awesome functionality … featured or not, it served great.

  • Well, here’s how to do it in 5.5.13:

    add_filter('acf/load_value/key= key_58c7ee3a37aee', 'default_value_ key_58c7ee3a37aee', 10, 3);
    function default_value_ key_58c7ee3a37aee($value, $post_id, $field) {
        if ($value === false) {
            $value = array(
                array(
                    'field_58984ddb5b8cc' => 'My title',
                    'field_58985045abac0' => 'My content',
                ),
                array(
                    'field_58984ddb5b8cc' => 'Another title for a content-less row',
                ),
            );
        }
        return $value;
    }

    Same syntax, only difference is you put it inside the appropriate filter. I prefer to use this code in the same file that I have exported the field in question.

  • This is working fine for me on ACF Pro 5.8.9

Viewing 11 posts - 1 through 11 (of 11 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.