|
cms:onGetTemplateFieldOptions event
Event handler signaturepublic mixed event_onGetTemplateFieldOptions(string $db_name, string $field_value)
Allows to populate drop-down, radio- or checkbox list fields, which have been added with cms:onExtendTemplateForm event.
Usually you do not need to use this event for fields which represent
data relations. But if you want a standard
field (corresponding an integer-typed database column, for example), to be rendered as a drop-down list, you should
handle this event.
The event handler should accept 2 parameters - the field name and a current field value. If the current field value is -1, the handler should return an array containing a list of options. If the current field value is not -1, the handler should return a string (label), corresponding the value. 01. public function subscribeEvents() 02. { 03. Backend:: $events ->addEvent( 'cms:onExtendTemplateModel' , $this , 'extend_template_model' ); 04. Backend:: $events ->addEvent( 'cms:onExtendTemplateForm' , $this , 'extend_template_form' ); 05. Backend:: $events ->addEvent( 'cms:onGetTemplateFieldOptions' , $this , 'get_template_field_options' ); 06. } 07. 08. public function extend_template_model( $template , $context ) 09. { 10. $template ->define_column( 'x_color' , 'Color' ); 11. } 12. 13. public function extend_template_form( $template , $context ) 14. { 15. $template ->add_form_field( 'x_color' )->renderAs(frm_dropdown); 16. } 17. 18. public function get_template_field_options( $field_name , $current_key_value ) 19. { 20. if ( $field_name == 'x_color' ) 21. { 22. $options = array ( 23. 0 => 'Red' , 24. 1 => 'Green' , 25. 2 => 'Blue' 26. ); 27. 28. if ( $current_key_value == -1) 29. return $options ; 30. 31. if ( array_key_exists ( $current_key_value , $options )) 32. return $options [ $current_key_value ]; 33. } 34. } See Also |