Support

Account

Home Forums Add-ons Repeater Field Dynamically populated Select within Repeater

Solved

Dynamically populated Select within Repeater

  • I have a Repeater field with 2 subfields, one of which is called “user_role”. I want to populate the options of that field with the current available roles in the right translation.

    I’ve used the acf/prepare_field filter to populate and know that it works, because it works flawlessly when the (Select-)field is NOT in a repeater. But Because I want to be able add as many user roles as needed, a repeater seems in place.

    The problem is that, once I’ve set the role in one or two repeater rows and save it, I get an error. As soon as I move my Select field into a repeater, after saving the field valueI get this:

    Notice: Undefined index: sub_fields in /www/kinsta/public/toolbox-dashboard/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-repeater.php on line 122

    Even if I remove my code after that, the error remains and I need to remove the entire field and start over. The subfield are nowhere to be found. After removing the repeater field and saving the fieldgroup, the subfields appear on the toplevel.

    Here’s an extract of my code, pretty simple really:

    class Options {
    
        public function __construct() {
    
            // make sure the user roles field has the appropriate roles
            add_filter( 'acf/prepare_field/name=tdb_user_role' , __CLASS__ . '::get_available_user_roles' );
    
        }
    
        public static function get_available_user_roles( $field ) {
            // setting these manually here, but will be coming from get_editable_roles()
            $field[ 'choices' ] = array( 'administrator' => 'Administrator' , 'editor' => 'Editor' );
            return $field;
        }
    }

    Filter is added after including the script and initializing it:

    new Options();

    Please, does anyone have a suggestion or insight on how to make this work within a repeater?

  • @jarvis I had already tried that, didn’t help me at the time. But because I know it SHOULD work I decided to simplify more and added what I was doing it to a brand new WP container. It turned out that it was something in my code, although I still don’t know why.

    In another class file I am retrieving a bunch of fields using a similar class contructor function. All other fields seemed to work just fine, but as soon as I try to get the option for a repeater field that has 2 (and probably more) fields it produced that error.

    Was able to work around it, used the repeater and role to forward to custom urls on wp_login action hook.

    Thank you for your time though.

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

You must be logged in to reply to this topic.