Programmatically populate select drop down

WordPress Event Management, Calendars & Registration Forums Frontend Submissions Programmatically populate select drop down

This topic contains 7 replies, has 3 voices, and was last updated by  Stephen Harris 7 years, 2 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #27176

    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
    #27183

    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
    #27189

    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
    #27190

    And that still doesn’t get it to work because it tells me that the value “is not a valid option” again.

    Brian Reeves
    #27202

    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
    #27222

    Awesome! Thanks, that worked.

    Brian Reeves
    #28535

    Hey Stephen, how do I apply this only one form?

    Ali Zamanian
    #28541

    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
Viewing 8 posts - 1 through 8 (of 8 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.