XF 2.1 [SOLVED] Can addon option of format "Select menu" have dynamic options?

asprin

Active member
In the screen below
1580532929264.webp
can I specify dynamic select options? The goal here is to show a dropdown menu consisting of all user groups created in the system. The validation callback bit seems of interest but doesn't look like it will achieve that.
 
You need to use the PHP callback format. There exists a few option render functions for usergroups already (look at the classes \XF\Option namespace).
 
Yes, did find a \XF\Option\UserGroup class. Looks like I can use the renderSelect function. But not sure how to reference it in the options page here. Does it have to be called in the validation callback? And whatever is returned from that will be used to draw the the select tag?

An example code would be really helpful please.
 
I've so far been able to grab the array of usergroups by using the following in the addon controller:

PHP:
public function fetchUserGroups()
{
       $group = $this->repository('XF:UserGroup');
       echo '<pre>';
       print_r($group->getUserGroupOptionsData(false));
       echo '</pre>';
}

which outputs the following:

PHP:
Array
(
    [3] => Array
        (
            [value] => 3
            [label] => Administrative
        )

    [4] => Array
        (
            [value] => 4
            [label] => Moderating
        )

    [2] => Array
        (
            [value] => 2
            [label] => Registered
        )

    [1] => Array
        (
            [value] => 1
            [label] => Unregistered / Unconfirmed
        )

)

Can this be used on the options page? If so how would one do that?
 
Oh right. Got it. Thanks to this post PHP Callback example code

Thanks Liam for pointing me in the right direction.

For reference, this is the screen:
1580549978278.png

Just need to figure out what array sub-options means. *, I assume, indicates ALL. But what can be put there apart from that.
 
Last edited:
The sub options indicates what keys are available inside the primary option array. It’d be used if you had multiple fields in a single option, for example. You’d list one per line. An asterisk means any is accepted.
 
Back
Top Bottom