Setting Default Values For MB Builder Custom Field Types

Support MB Builder Setting Default Values For MB Builder Custom Field TypesResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40506
    James CooperJames Cooper
    Participant

    I was able to setup a star rating field using your tutorial here: https://docs.metabox.io/tutorials/add-star-rating-fields/

    I was then able to add the star rating field type to the MB Builder using your documentation here: https://docs.metabox.io/extensions/meta-box-builder/#extending-the-builder

    
    add_filter( 'mbb_field_types', function ( $field_types ) {
    
        // Add star rating field type to Meta-Box Builder
        $field_types['star_rating'] = [
            'title'    => __( 'Star Rating', 'blogcraftco-theme' ),
            'category' => 'advanced',
            'controls' => [
                'name', 'id', 'type', 'label_description', 'desc',
                'clone', 'sort_clone', 'clone_default', 'clone_as_multiple', 'max_clone', 'add_button',
                'before', 'after', 'class', 'save_field', 'sanitize_callback', 'attributes', 'custom_settings',
            ],
        ];
        
        return $field_types;
    } );
    

    The star rating field works as expected when inserting it into any newly created field group.

    The one thing that I cannot seem to find is how to have the star rating be automatically set to a default value (i.e., 5) whenever the field group is initially loaded.

    Where/how would I do that for custom field types that have been added to the MB Builder?

    Best Regards,

    Ryan

    #40517
    PeterPeter
    Moderator

    Hello,

    Currently, there is no option to set the default value when adding a new field in the builder. I will inform the development team to consider supporting this in future updates.
    If you want to set the default value for the field when editing the post, please use the field setting std.

    #40525
    James CooperJames Cooper
    Participant

    I figured it out. I just added 'std' to the list of controls for the 'star_rating' field type (which activated the Default Value field in the MB Builder for the 'star_rating' field. I then set the Default Value to 5. Now, when I create a new review post, the rating is automatically set to 5 stars.

     // Add star rating field type to Meta-Box Builder
        $field_types['star_rating'] = [
            'title'    => __( 'Star Rating', 'blogcraftco-theme' ),
            'category' => 'advanced',
            'controls' => [
                'name', 'id', 'type', 'label_description', 'desc', 'std',
                'clone', 'sort_clone', 'clone_default', 'clone_as_multiple', 'max_clone', 'add_button',
                'before', 'after', 'class', 'save_field', 'sanitize_callback', 'attributes', 'custom_settings',
            ],
        ];
    #40641
    SamSam
    Participant

    Great thread, James (and Peter). Hopefully Metabox comes to support this natively in the near future, as this is a standard expectation of inputs nowadays.

    Sam

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.