Feature request – wordpress action/filter to populate ACF repeater fields

Closed

Import WP Addon - Advanced Custom Fields Updated 4 weeks ago 2 Replies

Victor asked 1 month ago on December 11, 2024 at 3:49 am

At the moment i’ve got a few more complex scenarios where i’d like to handle with custom-code (ie. adding in ACF Repeater field rows based on the data imported, potentially combined with other data obtained (ie. via an API / etc).

At the moment there’s only very limited visibility of a certain field & corresponding value, so I can’t build repeater rows myself (ie. a “Locations” column which has a variable number of rows: A, B, C, D, E, … Y, Z).

A wordpress action would be great where it’s passed the new/updated post id, along with the full row data (array $row) so I can create whatever sets of ACF repeater fields I need to (ie. [{‘name’: ‘A’, timezone: ‘+1:00’}, {‘name’: ‘B’, timezone: ‘+5:00’}, …] )

 

J

James replied Support Agent

1 month ago on December 12, 2024 at 4:48 pm

Hi Victor,

There is currently a undocument api (still in development untill all current addons are using this fully, then it will be documented) that will allow you to create template groups / fields and get the data, then you can do what you like with it. 

I have created a very basic example that will register a field group with 2 fields, and is repeatable as it sounds like you might need that (this will be added to every importer template by default).

/**
 * NOTE Custom importer addon class should be registered after plugins_loaded, hooked @ 9
 */

add_action('plugins_loaded', function () {

    class IWP_Addon_Example extends \ImportWP\Common\AddonAPI\ImporterAddon
    {

        /**
         * Register template fields
         * 
         * @param \ImportWP\Common\AddonAPI\Importer\Template\Template $template_data 
         * @return void
         */
        public function register($template_data)
        {
            // register a new template panel that is repeatable with id of acf_custom_repeater_data
            $panel = $template_data->register_panel('ACF Custom Repeater', [
                'id' => 'acf_custom_repeater_data'
            ])->repeater();

            // add basic text fields ( field_1 and field-b )
            $panel->register_field('Field A', ['id' => 'field_1']);
            $panel->register_field('Field B');
        }

        /**
         * Save record data
         * @param \ImportWP\Common\AddonAPI\Importer\ImporterData $data 
         * @return void 
         */
        public function save($data)
        {
            if ($panel = $data->get_panel('acf_custom_repeater_data')) {
                if ($panel_data = $panel->get_value()) {

                    // Panel data, since repeatable will be a multi dimensional array.
                    // print_r($panel_data);
                    /*
                    Array
                    (
                        [0] => Array
                            (
                                [field_1] => 1
                                [field-b] => 2
                            )

                    )
                    */



                    // get record id
                    // $record_id = $data->get_id();

                    // save data to custom field
                    // $data->update_meta('_my_custom_field_key', 'field value');
                }
            }
        }
    }

    new IWP_Addon_Example();
}, 9);

If you have any questions just let me know.

James

J

James replied Support Agent

4 weeks ago on December 20, 2024 at 4:19 pm

Hello Victor

This ticket is being marked as closed due to inactivity.

If this is not the case, please reply to this ticket to re-open it.

James