Custom Fields
Re-Envisioned
Support General Custom field that adds option groups (optgroup) to select field

  • Creator
    Topic
  • #13203
    Resolved pluginoven
    Participant

    Since the walk function was removed, the class described in thread:
    https://metabox.io/support/topic/option-group-support-for-select-select-advanced-field-types/
    no longer works. Here is the updated class for a new selectomatic field type that adds optgroup functionality:

    if ( class_exists( 'RWMB_Field' ) ) {
        class RWMB_Selectomatic_Field extends RWMB_Select_Field {
            public static function html( $meta, $field ) {
                    $options                     = self::transform_options( $field['options'] );
                    $attributes                  = self::call( 'get_attributes', $field, $meta );
                    $attributes['data-selected'] = $meta;
                    $walker                      = new RWMB_Walker_Selectomatic( $field, $meta );
                    $output                      = sprintf(
                        '<select %s>',
                        self::render_attributes( $attributes )
                    );
                    if ( ! $field['multiple'] && $field['placeholder'] ) {
                        $output .= '<option value="">' . esc_html( $field['placeholder'] ) . '</option>';
                    }
                    $output .= $walker->walk( $options, $field['flatten'] ? -1 : 0 );
                    $output .= '</select>';
                    $output .= self::get_select_all_html( $field );
                    return $output;
            }
        }
    
        class RWMB_Walker_Selectomatic extends RWMB_Walker_Select {
    
            public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
                $meta = $this->meta;
                if($depth){
                    $output .= sprintf(
                        '<option value="%s" %s>%s</option>',
                        esc_attr( $object->value ),
                        selected( in_array( $object->value, $meta ), true, false ),
                        esc_html( RWMB_Field::filter( 'choice_label', $object->label, $this->field, $object )
                        )
                    );
                }
                else{
                    $output .= sprintf(
                        '<optgroup label="%s">',
                        esc_html( RWMB_Field::filter( 'choice_label', $object->label, $this->field, $object )
                        )
                    );
                }
            }
    
            public function rwmb_end_html_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
                if(!$depth){
                    $output .= '</optgroup>';
                }
            }
        }
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.