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
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
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
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
Of course, you could use the form customiser UI too.

Stephen Harris
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
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