A dedicated radio button for Gravity Form with the ability to upload an icon

39 Views Asked by At

Of course, here's how you can request help for creating a custom radio button field in Gravity Forms that allows uploading an image as an icon next to the label, formatted for Stack Overflow:


I'm in need of assistance in creating a custom radio button field for Gravity Forms that enables the upload of an image to serve as an icon next to the label. I'd appreciate any guidance or code examples related to this.

Thank you!

if (class_exists('GF_Field')) {
    class CustomRadioButton extends GF_Field {
        public $type = 'custom_radio_button';
        
        public function get_form_editor_field_title() {
            return esc_attr__('Custom Radio Button', 'txtdomain');
        }
        
        public function get_form_editor_button() {
            return [
                'group' => 'advanced_fields',
                'text'  => $this->get_form_editor_field_title(),
            ];
        }
        
        public function get_form_editor_field_settings() {
            return [
                'label_setting',
                'description_setting',
                'choices_setting', // Adding choices setting
                'css_class_setting',
                'conditional_logic_field_setting',
            ];
        }
        
        public function get_field_input($form, $value = '', $entry = null) {
            if ($this->is_form_editor()) {
                return '';
            }
            
            $id = (int) $this->id;
            
            $radios = '';
            foreach ($this->choices as $key => $choice) {
                $image_src = isset($choice['image_src']) ? esc_url($choice['image_src']) : '';
                $link = isset($choice['link']) ? esc_url($choice['link']) : '#';
                
                $radios .= '<div>';
                $radios .= '<input type="radio" id="input_' . $id . '_' . $key . '" name="input_' . $id . '" value="' . $key . '">';
                $radios .= '<label for="input_' . $id . '_' . $key . '"><img src="' . $image_src . '" alt="' . $choice['text'] . '"></label>';
                $radios .= '</div>';
            }
            
            return $radios;
        }
    }
    GF_Fields::register(new CustomRadioButton());
}
0

There are 0 best solutions below