Want to allow selection from a dynamically generated list of items

I am building a module that has a nav menu that filters and displays content boxes. The list is built, item by item, in the module admin panel. Each content box is built within the module, and I would like to be able to have a dropdown menu within the content box builder form that is dynamically generated from the list items created previously.

This is what I’ve put together…didn’t work. Am I moving in the right direction and just have syntax errors or is this completely wrong?


'general' => array(
    'title'         => '',
    'fields'        => array(
        'partner_type' => array(
            'type'          => 'select',
            'label'         => __( 'Select Partner Type', 'fl-builder' ),
            'default'       => '',
            'options'       => array(
                for ( $i = 0; $i < count( $settings->nav_list_items); $i++) : if (empty( $settings->nav_list_items[ $i ] ) ) continue;
                echo $settings->nav_list_items[ $i ]      => __( $settings->nav_list_items[ $i ], 'fl-builder' ),
                endfor;
        )
    )
),

Hey Brain,

Welcome to the BB forums! :slight_smile:

I’ve already assigned another member of the team to assist you with your concern.

Ben

Hey Brian,

It does look like you have some syntax errors. Specifically, I don’t think you can do a for statement within an array like that. Your best bet will most likely be to create a custom field. Have you looked at how to do that in the module docs?

http://forum.wpbeaverbuilder.com/custom-module-documentation

Let me know if you have any questions about that.

Justin

To piggyback on this – is there a way to reference the object settings in the form?

To extend my previous question – just like the OP I’d like to create an array of options based on input from other fields, and use them in a custom field.

The problem is that my custom field is in a (multiple) form inside a form, and $settings available in the control is limited to that forms settings, and not the full $settings object. Same thing when using fl_builder_render_settings_field filter.

Is there a way to get the parent $settings? If not, could you add the parent $settings as another var included in the fl_builder_control_* action?

Hey Josh,

I just checked and $settings is available as a fourth parameter in the fl_builder_control_ action, but it’s undocumented. Sorry about that!

Does that help?

Justin

Yes an no. $settings only displays the current form settings, so if it is a form within a form, I’m missing the parent form $settings. I guess I could use the update function to push an array of options to all child forms, but that seems silly.

Ah, gotcha. Nested forms are the issue here. Would it help if you could get the parent settings like this in your function…

$parent = FLBuilderModel::get_node( $_POST['node_id'] );

$parent_settings = $parent->settings;

Let me know and I can get a patch together that makes that possible. Right now you can’t access the node id in the post variables for nested forms.

Justin

That would be great!

Ah, crap. The only thing is that most of the builder JS is now concatenated and minified in fl-builder.min.js unless you have WP_DEBUG on. If you have that on, you can add this below line 5533 of fl-builder.js…

node_id: form.attr('data-node'),

Like so…

FLBuilder.ajax({
	action: 'render_settings_form',
	node_id: form.attr('data-node'),
	type: type,
	settings: settings.replace(/&#39;/g, "'")
},

Once that’s done you can get the parent settings as described above. I’ll make sure to get that in the next update.

Thanks,
Justin

Thanks for the update, Justin. One more quick question – is there a way to grab the preview settings? Right now it’s grabbing the saved settings.

Ah, right. To do that you’ll need to add this below the last line you added…

node_settings: FLBuilder._getSettings(form),

That will pass the current settings that are being previewed. You can access those using…

$post_data = FLBuilderModel::get_post_data();

$parent_settings = $post_data['node_settings']

I’ll get that in the next update.

Justin