carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Check if field exists not possible?

Open JRDN40 opened this issue 4 years ago • 1 comments

Hey, im trying to build a plugin and need first to setup some field markups. So on the first page the user should be able to create some markup, for this i use the complex field. So it should look like this for example: processor -brand -model -core gpu -brand -model -memory ... The user should be able to add more components or less. I want to use this markup on a second page, so that the user can just add some processors for example and fill in the fields created based on the markups.

But my problem is, if i try to get the fieldnames of the third markup for example, it throws an error, if there are only 2 markups: Is there any possbility to check if a field exists before trying to get its data?

this is my code: ( i added a comment where the problem is)

function configurator_setup_page() {
    global $setuphtml_description;
    global $attribute_labels;

    Container::make( 'theme_options', 'setupp')
    ->set_page_menu_title( 'Configurator' )
   ->add_fields( array(
        Field::make( 'html', 'crb_information_text' )
        ->set_html( $setuphtml_description ),
        Field::make( 'complex', 'setup' )

                ->add_fields( array(
                    Field::make( 'text', 'slug' ),
                    Field::make( 'complex', 'crb_eigenschaften' )
                    ->setup_labels(  $attribute_labels )
                    ->add_fields( array(
                        Field::make( 'text', 'eigenschaft' )
                        ->set_width( 50 ),
                    ) )

                ) )
                    ));

     }
    function configurator_admin_menu_site() {
        global $facts_labels;

        Container::make( 'theme_options', ( 'Fakten & Regeln' ) )
        ->set_page_parent( 'crb_carbon_fields_container_setupp.php' )

        
        ->add_tab( __( 'Wissensbasis' ), array(
            Field::make( 'complex', 'crb_fakten' )
            ->add_fields( carbon_get_theme_option('setup[0]/slug'), array(
                Field::make( 'text', carbon_get_theme_option('setup[0]/crb_eigenschaften[0]/eigenschaft')),
                Field::make( 'text', carbon_get_theme_option('setup[0]/crb_eigenschaften[1]/eigenschaft')),
                Field::make( 'text', carbon_get_theme_option('setup[0]/crb_eigenschaften[2]/eigenschaft')), // first check if field exists <----------------------------------


            ) )
            ->add_fields( 'movie', array(
                Field::make( 'text', 'length', __( 'Length' ) ),
                Field::make( 'text', 'title', __( 'Title' ) ),
                Field::make( 'file', 'video', __( 'Video' ) )
            ) )
        ) )
        ->add_tab( __( 'Regeln' ), array(
            
            Field::make( 'text', 'crb_email', __( 'Notification Email' ) ),
            Field::make( 'text', 'crb_phone', __( 'Phone Number' ) ),
            
            
        ) );
    }

JRDN40 avatar Nov 08 '21 11:11 JRDN40

Have you considered just using carbon_get_theme_option('setup') and use the actual data to continue building your fields?

dhalucario avatar Dec 16 '21 21:12 dhalucario

Hi @JRDN40 ,

You could try doing something like this:

$dynamic_fields = [];

$setup = carbon_get_theme_option( 'setup' );

foreach ( $setup as $setup_field ) {
    $dynamic_fields[] = Field::make( 'text', $setup_field['crb_eigenschaften']['eigenschaft'] );
}

function configurator_admin_menu_site() {

Then in the ->add_files method, you could use the new array that you've created:

 ->add_tab( __( 'Wissensbasis' ), array(
            Field::make( 'complex', 'crb_fakten' )
            ->add_fields( carbon_get_theme_option('setup[0]/slug'), $dynamic_fields )

Please let us know if there is anything else, that we could help with.

HTMLBurger-NG avatar Feb 20 '23 09:02 HTMLBurger-NG