Carbon-fields: Change CF metabox title

Created on 20 Feb 2017  路  4Comments  路  Source: htmlburger/carbon-fields

Hello, I'm using the wonderful Carbon Fields in my project. I have a text field to place a title, and would like that when the text field was filled in, the metabox's tag would be changed by it.

Field::make('text', 'main_block_title',__('Block Title', 'textdomain'))

My English is bad, so I took a picture for an example.

example

Could you help me?

good first issue [type] question

All 4 comments

Hi @jquimera ,

Thank you for the kind words!

As for your use case, there isn't a dynamic method built-in to change the container label as you are typing in but it can reflect your changes after you save the post. Here's a rough example:

global $pagenow;

$container_name = 'Block Posts Listing';
if ( is_admin() && $pagenow === 'post.php' && isset( $_GET['post'] ) ) {
    $post_id = intval( $_GET['post'] );
    $post_container_name = carbon_get_post_meta( $post_id, 'main_block_title' );
    if ( ! empty( $post_container_name ) ) {
        $container_name = $post_container_name;
    }
}
Container::make( 'post_meta', $container_name )
    ->add_fields( array(
        // your container fields here
    ) );

_Note: this is not exactly recommended but it should work._

@atanas-angelov-dev This is amazing! Thanks. Could this solution also work with the "complex"?

Using your solution, I tried to do the same with the "Complex Field" but the field is not being changed.

$block_name = 'My Block Title';
if ( is_admin() && $pagenow === 'post.php' && isset( $_GET['post'] ) ) {
    $post_id = intval( $_GET['post'] );
    $block_container_name = carbon_get_post_meta( $post_id, 'block_title' );
    if ( ! empty( $block_container_name ) ) {
        $block_name = $block_container_name;
    }
}
Container::make( 'post_meta', 'Blocks Builder' )
    ->show_on_post_type('page')
    ->add_fields( array(

        Field::make( 'complex', 'blocks_posts', __( 'Blocks Posts' ) )
            ->setup_labels( array(
                'plural_name' => __( 'Blocks' ),
                'singular_name' => __( 'Block' ),
            ) )
            ->add_fields('block', $block_name, array(
            Field::make('text', 'block_title','Title')
            )),

) );

You have to get the value of the complex field so you have to call carbon_get_post_meta( $post_id, 'blocks_posts', 'complex' ) and then get the "block_title" value from the returned array (if any value is stored for it).
Use print_r() on the value to see how complex values are returned.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmhendricks picture dmhendricks  路  3Comments

broskees picture broskees  路  4Comments

bjoernhasse picture bjoernhasse  路  3Comments

ArekZw picture ArekZw  路  4Comments

proweb picture proweb  路  3Comments