Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I pull in theme options within the carbon_fields_register_fields action? #1194

Open
Julius151 opened this issue Oct 29, 2023 · 0 comments

Comments

@Julius151
Copy link

Julius151 commented Oct 29, 2023

Version

  • Carbon Fields: 3.6.0
  • WordPress: 6.3.2
  • PHP: 8.1

Expected Behavior

I have a "theme_options" container with two complex fields.

The first complex field if for creating categories.
In the second group I want to pull those categories and make them options for one of the fields of the second group, called products.

Actual Behavior

No matter what I tray, within my class I can not pull in the $catgeories array, no matter which hook I use to get the data.

My Class:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

class CarbonFields {

    private $categories;

    public function init(){
        add_action( 'init', [$this, 'get_options'] );
        add_action( 'carbon_fields_register_fields', [$this, 'crb_attach_theme_options'] );
        add_action( 'after_setup_theme', [$this, 'crb_load']  );
    }


    public function get_options(){
        $this->categories = carbon_get_theme_option( 'categories' );

        error_log('### -  $this->categories get options - ###');
        error_log(print_r( $this->categories, true));

    }

    public function crb_attach_theme_options() {
        
        error_log('### -  $this->categories - ###');
        error_log(print_r( $this->categories, true));

        $product_labels = array(
            'plural_name' => __( 'Produkte'),
            'singular_name' => __( 'Produkt' ),
        );
        $category_labels = array(
            'plural_name' => __( 'Kategorien'),
            'singular_name' => __( 'Kategorie' ),
        );

        // carbon_get_theme_option( 'crb_text' );
        Container::make( 'theme_options', __( 'Produkte', 'ekiosk' ) )
            ->set_page_menu_position( 2 )
            ->set_icon( 'dashicons-images-alt' )
            ->add_fields( array(
                Field::make( 'complex', 'categories', 'Kategorien' )
                    ->setup_labels( $category_labels )
                    ->set_layout( 'tabbed-vertical' )
                    ->add_fields( array(
                        Field::make( 'text', 'category', 'Kategorie' ),
                    ))
                    ->set_header_template( '
                        <% if (category) { %>
                            Kategorie: <%- category %>
                        <% } else{ %>   
                            Kategorie
                        <% } %>
                    ' )
            ) )
            ->add_fields( array(
                Field::make( 'separator', 'crb_separator', __( 'Produkte' ) )
            ) )
            ->add_fields( array(
                Field::make( 'complex', 'products', 'Produkte' )
                    ->setup_labels( $product_labels )
                    ->set_layout( 'tabbed-vertical' )
                    ->set_collapsed( true )
                    ->add_fields( array(
                        Field::make( 'text', 'title', 'Titel' ),
                        Field::make( 'image', 'image', 'Bild' ),
                    )),
            ) );

    }
    
    public function crb_load() {
        require_once( __DIR__ . '/../../vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
    }
}

Comments

How can I accomplish this goal? I have been trying for two hours to get it to work, any help would be much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant