Add custom form element via theme to booking form

WordPress Event Management, Calendars & Registration Forums General Question Add custom form element via theme to booking form

This topic contains 6 replies, has 2 voices, and was last updated by  Stephen Harris 9 years, 10 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #16584

    What a great release of the latest event-organiser plugins … I was very pleased to see a eo_get_the_occurrence_id() and the booking-form enhancements.

    But I have one question. If I add a custom form element to the default booking-form it gets displayed below the submit button.

    I added the custom element via add_action( 'eventorganiser_get_event_booking_form', 'add_field_to_booking_form', 10, 2 ); as explained under http://wp-event-organiser.com/blog/announcements/event-organiser-pro-1-10-0/.

    How can I put this element above the submit button?

    • This topic was modified 9 years, 10 months ago by  B+eo.
    B+eo
    #16592

    Yes, the documentation is lacking at the moment, but you can specify the position in a settings array. You an also specify a parent (for elements which allow nested elements – currently only fieldset elements).

     $form->add_element( $checkbox, array(
         'at'     => $position,  //integer 
         'parent' => $parent_element, //instance of iEO_Booking_Form_Element_Parent
     );        
    

    Of course, you need to determine the appropriate $position and $parent_element. You can do this by using an existing element (e.g. the submit button) to position this new element relative to that element.

    Here’s an example which places a checkbox just before the submit button:

     add_action( 'eventorganiser_get_event_booking_form', 'add_field_to_booking_form', 10, 2 );
     function add_field_to_booking_form( $form, $event_id ){
    
         //Create a field
         $checkbox = EO_Booking_Form_Element_Factory::create( array(
             'type'        => 'checkbox',
             'id'          => 'my-unique-id', //Must be unique in the form. You should not use 'name', 'ticketpicker', 'gateway' or 'email' as an ID,
             'label'       => 'Select an option:',
             'options'     => array( 'A' => 'Option A', 'B' => 'Option B', 'C' => 'Option C' ),
             'required'    => true, //Default is false
             'description' => "Pick one of the options",
         ) );
    
         $submit   = $form->get_element( 'submit' );
         $position = intval( $submit->get( 'position' ) ); //position of submit button.
         $parent   = $submit->get_parent(); //parent of submit button
    
         $form->add_element( $checkbox, array(
             'at'     => $position, 
             'parent' => $parent_element,
         );  
    
     }
    

    Just an explanatory note about the ‘at’ setting. The added element is inserted at that position. Any existing element in that position is moved forward (i.e. pushed down the form). If you wanted to add something immediately after the submit button you would at it at $postion + 1.

    Hope that helps!

    Stephen Harris
    #16614

    Thank you very much.

    Another question: Is there an easier way to rearrange the required default elements than removing them and recreating them?

    B+eo
    #16615

    Yes, simply remove and re-add the element:

    $element = $form->get_element( 'name' );
    $form->remove_element( $element );
    $form->add_element( $element, array( 'at' => ..., 'parent' => ... ) );
    
    Stephen Harris
    #16616

    Of course, you could use the form customiser UI too.

    Stephen Harris
    #16617

    Oh, I see, but in this way I can’t change attributes of the element like for example remove the last name. Or can I?

    B+eo
    #16618

    Good point, the form customiser allows you to require a last name (or not), but not to remove it entirely. To remove the last name field:

     $element = $form->get_element( 'name' );
     $element->set( 'lname', false );
    
    Stephen Harris
Viewing 7 posts - 1 through 7 (of 7 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.