I need to programmatically populate a select drop down field on an FES form. I don’t see a hook for doing that so I did it with javascript. That causes an error on submission telling me that the value is invalid. How can I properly populate the drop down options and values?
Brian Reeves
Hi Michael,
You can use the eventorganiser_get_fes_form
– which is fired when ever the event form is instantiated. It allows you to modify the form object or its elements.
In the below I’ve assumed that you’ve got a select element with ID 9. Change that ID accordingly, or you can even create a new element at this point.
add_action( 'eventorganiser_get_fes_form', function( $form ) {
$dropdown = $form->get_element( 9 );
if ( ! $dropdown ) {
return;
}
$dropdown->set( 'options', array(
'foobar' => 'Foobar',
'hello-world' => 'Hello World',
) );
} );
Stephen Harris
Thanks. That helped a lot. Now I have an issue where I need it to be an associative array but I’m using a number (post id) as the value. I have to comment out the below in class-eo-event-form-element-view.php line 129
because it sees the integer and thinks it’s non associative.
//if ( $is_associative ) {
$select_options = $this->element->get( 'options' );
//} else {
// $select_options = array_combine( $this->element->get( 'options' ), $this->element->get( 'options' ) );
//}
Brian Reeves
And that still doesn’t get it to work because it tells me that the value “is not a valid option” again.
Brian Reeves
Hi Brian,
There are associative checks in class-eo-event-form-element.php
too. The associative checks are too lax here, as noted they are treating anything that has numerical only keys as non-associative. But that arrays such as:
array(
1 => 'foobar',
4 => 'hello world'
);
are actually associative (or at least, for the purposes of the form element).
I shall include this in the next update, but an immediate patch would be to replace the definition of $is_associative
in the lines above the one you posted with:
is_array( $options ) && ( array_keys( $options ) !== range( 0, count( $options ) - 1) );
Then in class-eo-event-form-element.php
there are three methods, is_associative()
, the return statement should be:
return is_array( $options ) && ( array_keys( $options ) !== range( 0, count( $options ) - 1) );
Stephen Harris
Awesome! Thanks, that worked.
Brian Reeves
Hey Stephen, how do I apply this only one form?
Ali Zamanian
The passed EO_Event_Form
instance has and id property: $form->id
. Or you can use $form->get('name')
to identify the form (this assumes you have given it a unique name).
Stephen Harris