Support

Home Forums Event Espresso Premium Dynamic Select box options

Dynamic Select box options

Posted: September 5, 2019 at 3:24 pm


a608237

September 5, 2019 at 3:24 pm

In my site, members have buddypress groups. When they registered for an event, I’d like a custom question to be a dropdown select box populated by all the buddypress groups that the primary user is in. Is there a hook to alter the options for custom quetions? Where is the logic for custom questions/select boxes handled?

THanks!


Josh

  • Support Staff

September 5, 2019 at 7:44 pm

Hi,

The logic is in the generate_form_input() method. Here’s a link to its source:

https://github.com/eventespresso/event-espresso-core/blob/master/core/db_classes/EE_Question.class.php#L502

You’ll find there’s a filter hook at the return which should allow for altering the options.


a608237

September 6, 2019 at 12:05 pm

Hi Josh,

Thanks for the the guidance. I might be misunderstanding the proper way to do this but:

I see in generate_form_input():

switch ($this->type()) {
            // Dropdown
            case EEM_Question::QST_type_dropdown:
                $result = new EE_Select_Input($this->options(), $input_constructor_args);
                break;

The method options() contains the dropdown select choices correct? In the apply_filters('FHEE__EE_Question__generate_form_input__return', $result, $registration, $this, $answer);, options is not in there. How would one modify the choices (options? method) then? I’ve only just started understanding php/hooks etc. Thanks for your insight.


Josh

  • Support Staff

September 6, 2019 at 12:35 pm

Normally options() will get the dropdown select choices, but in this case, you don’t want options(), you want your custom buddypress group options instead.

Basically the filter hook allows you to programmatically swap in a different return value. Here are a few references that explain the basics of using filter hooks:

https://wordpress.org/support/topic/fliters-vs-actions-from-a-newbe/#post-1366594
and
https://pippinsplugins.com/a-quick-introduction-to-using-filters/


a608237

September 6, 2019 at 7:18 pm

Thanks for those resources, they’re super informative. But, I think there are some slight object oriented differences from those examples and my end goal that I can’t wrap my head around. As a test, I tried to pass an array of hard coded strings as options() like:

add_filter('FHEE__EE_Question__generate_form_input__return','EE_selectbox');
function EE_selectbox($this) {
 $this->options()= [
    "foo" => "bar",
    "bar" => "foo",
];
  return $this->options();
}

This returns an error saying $this can’t be used. I’ve tried subbing it out with EE_Question::options() but that’s not working either.. Am I approaching this right? I have a feeling my foo bar key values array ain’t going to work either..is the array set to be properly inserted as select box choices?

Thanks in advance.


naddi

September 7, 2019 at 3:27 am

Hi a608237, this is a bit off topic for this thread, but I wasn’t sure how else to reach you. I’m trying to achieve the same thing you did here:

Update Buddypress custom xprofile field from EE event registration form

https://eventespresso.com/topic/update-buddypress-custom-xprofile-field-from-ee-event-registration-form/

Could you please email me at bsayler@naddi.org to discuss? Thanks, you’d be a lifesaver!


Tony

  • Support Staff

September 9, 2019 at 6:32 am

The pseudo-variable $this is basically a reference to an instance of an object from within itself, see:

https://www.php.net/manual/en/language.oop5.basic.php#example-172

Note that the hook you are using is passed multiple variables, this is how its called in EE:

apply_filters('FHEE__EE_Question__generate_form_input__return', $result, $registration, $this, $answer);

Meaning any functions you hook into that filter have access to those if you set the hook up correctly, for example:

add_filter('FHEE__EE_Question__generate_form_input__return','EE_selectbox', 10, 4);
function EE_selectbox($result, $registration, $question, $answer) {
    // Do something with any/all/none of the pass variables.
}

However, none of what you are currently using will work, not only due to the use of $this but options() is a method and you’re trying to assign values to it.

Then it appears what you are trying to do is return just the values you assigned earlier, but at that point, EE is expecting a form input, not a list of option values. You could have generated an input and set the option values, then returned the input at that point but not just the values themselves.

I also think you’re going to run into further issues with what you are trying to do as anywhere that tries to pull in the values in the ‘standard’ way for that question is going to fail, you won’t have the relationships with question options and so on so you’re likely going to add code for this to work.

I’d recommend contacting a developer familiar with OOP and/or Event Espresso as what your trying to do isn’t as simple as it may seem.


a608237

September 12, 2019 at 9:19 am

Hi Tony,

I want to give this a more serious effort before aborting. My premium status expires in 3 months so I’d like to go as far as I can while I can still get help.

Can you provide a simple example function of:
You could have generated an input and set the option values, then returned the input at that point but not just the values themselves.

What is meant by the ‘standard’ way in which Event Espresso tries to pull values?


Tony

  • Support Staff

September 12, 2019 at 9:38 am

I want to give this a more serious effort before aborting. My premium status expires in 3 months so I’d like to go as far as I can while I can still get help.

Just to clarify, your support license does not provide support for customizations.

We are often fairly relaxed with this and will help point you in the right direction when we can, but we won’t write custom code for you, thats not covered under ‘Support’.

Can you provide a simple example function of:

The point of the above was intended to highlight the fact that the function is doing it wrong and returning completely the wrong values for what they filter is for.

If you view where that code is applied, you can see how the inputs are generated:

https://github.com/eventespresso/event-espresso-core/blob/master/core/db_classes/EE_Question.class.php#L657

Just above that filter shows how EE generates the inputs for all of the different input types.

I can’t really provide a better example of how EE generates the inputs than EE itself. You really need someone familiar with OOP and EE to work through this for you.

What is meant by the ‘standard’ way in which Event Espresso tries to pull values?

If EE generated an input, it would have ‘options’ for those inputs saved within the database. You want to completely bypass that and use your options own based on dynamic values for each user, meaning your no longer using the ‘standard’ relationships with questions and options in the DB, your using your own.

Anywhere that relies on that relationship (because that is how EE questions are set up by default) may well need filtering to use your new values.

The support post ‘Dynamic Select box options’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso