Support

Account

Home Forums Add-ons Repeater Field Creating default subfields in repeater field

Solved

Creating default subfields in repeater field

  • Hi Team,

    First off – thank you for such a awesome plugin. We use this plugin on multiple high-end clients and this has saved us on time, coding, and headaches!

    Question we hope you can help us with…

    We’re creating custom field types (using a repeater field as the base), but would like to add default subfields to it.

    For example:
    Our custom field type is called: “Product”. The field type is a repeater field – that we would like to have an image field, text field and text area already added/rendered without having to select them in the subfield. It seems this is possible – we just do not know where to hook into.

    If you can provide a sample hook for the text field – I know we can just do the rest.

    Currently we are using acf_add_local_field, while it does load on the Post Edit page, we do not see the subfields on the ACF Field Group page. Are we doing this right?… or is this something we will need to hook into acf_load_field?

    Any help is appreciated.

    Thanks!

  • I just want to be sure I understand what you’re trying to do. Could be I’m just dense today but your question has me confused.

    When you create a new post that has the repeater field do you have an add row button and when it’s clicked there are sub fields to enter information into? or do you mean that there are no sub field when clicking the add row button?

    I’m trying to figure out by your comments if you’re trying to create the sub fields or trying to add default content to fields that are already there.

  • @john – Thanks for the reply. I was able to kind of find a solution for this in this thread here: load_field – repeater adds empty fields to Field Group, however there is an issue with ACF adding multiple empty fields on Field Group update when having an array inside of 'sub_fields' => array().

    It would be best if I close this and continue on the other thread in hand.

    Thanks.

    But to answer you question – I am trying to create default subfields without having to add them manually via ACF.

  • I actually think I asked a related question a while back. I posted a partial solution. I was doing research on possible solutions for a project that never happened. But it may be useful. http://support.advancedcustomfields.com/forums/topic/dynamically-generate-subfield-content/

  • Yes, I saw that post also ;). So the code I added in the other thread does work, it just adds multiple empty fields when you add another field to that Field Group. I just cannot figure out why. It only happens to the sub_fields $field though.

  • I was just looking at the code in that old thread, it seems that breaks ACF now, must have been some changes in the last year.

    Before I comment in your other thread, I’ve been working on code to load default values into a repeater and I just want to make sure I’m doing the same thing you’re doing.

    You are trying to load default values for a new post, not set values for all posts? the load field filter will change existing posts as well as populating values for a new post. I think what you are looking for is still the acf/load_value hook, but it needs to be populated differently.

  • What I am actually doing is just loading default subfields (with no values) into a repeater. Just empty subfields the user can field out.

    So basically, I am pre-adding subfields to a repeater, so I do not have to add the fields manually.

    The subfields are created automatically using acf/load_field/type=flexible_content.

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text'
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Now, this works perfectly. Keep in mind – doing it this way does not allow you to add child fields via ACF. You will have to add it within the same acf/load_field/type=repeater hook.

    I believe you want to add default values – which you can do like this:

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text',
          'default_value' => 'Hi' // ADD THIS LINE
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Test it out in functions.php if that is what you are trying to achieve.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.