Support

Account

Forum Replies Created

  • Well in my case I am using acf/load_field to load form fields dynamically for each post in a loop, similar to this example:

    if( $loop->have_posts() ):
        while( $loop->have_posts() ): $loop->the_post();
        $id = get_the_ID();
        if( function_exists('acf_add_local_field_group') ):
        acf_add_local_field_group(array(
          'key' => 'field_product_item_' . $id,
          'title' => 'Product Item #' . $id,
          'fields' => array(
            array(
              'key' => 'field_product_item_group_' . $id,
    	  'type' => 'group',
              'sub_fields' => array()
               //... dynamic items with default values will be added to this sub_field using acf/load_field
               )
            )
         );
        add_filter('acf/load_field/key=field_product_item_group_' . $id, 'load_product_details_form', 20, 3);
    

    So this approach seems to work so far. Anyway I hope this is not prone to problems in the future.

  • Hi John,

    Normally, I was following field keys starting with ‘field_’ string for being orderly, but I didn’t know that it was important for avoiding problems. Anyway I will pay extra attention to that point, thanks.

    As for loading values for the dynamic form, I have been using acf/load_field filter in a loop and using default_values field to insert the values from the third party API. So when the form is initialized fields are populated with default values. After that user makes necessary changed if needed, saves the form, and new values gets stored to database. In a way problem looks solved for now. (But I haved noted that I need to use acf_update_field($field) before returning $field in order to get default_value to stored to DB)

    Please let me know is this a bad practice. I am doing something similar to this:

    $new_fields = array(
      array(
        'key' => 'field_product_item_title' . $item_id,
        'name' => 'field_product_item_title' . $item_id,
        '_name' => 'field_product_item_title' . $item_id,
        'label' => 'Product Title',
        'type' => 'text',
        'default_value' => $title_from_third_party_api
        //... (instructions, conditional_logic, required etc)
      ),
      //... add rest of the field items
    );
    
    foreach($new_fields as $index=>$new_field){
      array_push($field['sub_fields'], $new_field);
    }
    
    acf_update_field($field);
    return $field;
    

    Thanks!

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