Created
December 19, 2017 02:58
-
-
Save jrstaatsiii/87532421e2062731e8783145dc8244b5 to your computer and use it in GitHub Desktop.
Quick Example of how we might organize our Carbon Fields setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'ssm_remove_post_type_support' ); | |
function ssm_remove_post_type_support() { | |
remove_post_type_support( 'page', 'editor' ); | |
} | |
add_action( 'after_setup_theme', 'crb_load' ); | |
function crb_load() { | |
require_once( 'vendor/autoload.php' ); | |
\Carbon_Fields\Carbon_Fields::boot(); | |
} | |
function styles_partial( $additional_fields = array() ) { | |
$sep = array ( | |
Field::make( 'separator', 'style_options', 'Style' ) | |
); | |
$styles = array ( | |
Field::make( 'text', 'html_id', 'ID' ), | |
Field::make( 'text', 'html_styles', 'Classes' ), | |
); | |
return $styles_partial = array_merge( $sep, $additional_fields, $styles ); | |
} | |
function admin_partial() { | |
$sep = array ( | |
Field::make( 'separator', 'admin_options', 'Admin' ) | |
); | |
$admin = array ( | |
Field::make( 'checkbox', 'active', 'Active' ), | |
); | |
return $admin_partial = array_merge( $sep, $admin ); | |
} | |
function header_component_group() { | |
$header_component_group = array ( | |
Field::make( 'text', 'headline', 'Headline' ), | |
Field::make( 'text', 'subheadline', 'Subheadline' ), | |
); | |
$additional_fields = array ( | |
Field::make( 'radio', 'alignment', 'Alignment' ) | |
->add_options( array( | |
'align-left' => 'Left', | |
'align-center' => 'Center', | |
'align-right' => 'Right', | |
) ) | |
->set_classes( 'inline' ) | |
); | |
return $header_component_group = array_merge( $header_component_group, styles_partial( $additional_fields ), admin_partial() ); | |
} | |
function header_component_header_template() { | |
return | |
'<% if (headline) { %> | |
Headline: <%- headline %> | |
<% } else { %> | |
Headline | |
<% } %>'; | |
} | |
function editor_component_group() { | |
$editor_component_group = array ( | |
Field::make( 'rich_text', 'editor', '' ), | |
); | |
return $editor_component_group = array_merge( $editor_component_group, styles_partial(), admin_partial() ); | |
} | |
function form_component_group() { | |
$form_component_group = array ( | |
Field::make( 'gravity_form', 'form', '' ), | |
); | |
return $form_component_group = array_merge( $form_component_group, styles_partial(), admin_partial() ); | |
} | |
function template_one_module() { | |
$template_one_module = array ( | |
Field::make( 'text', 'template_text', 'Template Text' ), | |
Field::make( 'rich_text', 'template_rich_text', '' ), | |
); | |
} | |
add_action( 'carbon_fields_register_fields', 'crb_attach_post_meta' ); | |
function crb_attach_post_meta() { | |
$module_labels = array( | |
'plural_name' => 'Modules', | |
'singular_name' => 'Module', | |
); | |
$component_labels = array( | |
'plural_name' => 'Components', | |
'singular_name' => 'Component', | |
); | |
$column_labels = array( | |
'plural_name' => 'Columns', | |
'singular_name' => 'Column', | |
); | |
Container::make( 'post_meta', 'Layout Builder' ) | |
->where( 'post_type', '=', 'page' ) | |
->add_fields( array( | |
Field::make( 'complex', 'modules', '' ) | |
->set_collapsed( true ) | |
->setup_labels( $module_labels ) | |
->add_fields( 'template_one', 'Template One', template_one_module() ) | |
->add_fields( 'template_two', 'Template Two', header_component_group() ) | |
->add_fields( 'template_three', 'Template Three', header_component_group() ) | |
->add_fields( 'columns', 'Column(s)', array( | |
Field::make( 'complex', 'columns_module', '' ) | |
->set_layout( 'tabbed-horizontal' ) | |
->setup_labels( $column_labels ) | |
->set_min(1) | |
->set_max(4) | |
->add_fields( 'column_list', '', array( | |
Field::make( 'complex', 'components', '' ) | |
->set_collapsed( true ) | |
->setup_labels( $component_labels ) | |
->add_fields( 'header', 'Header', header_component_group() ) | |
->add_fields( 'text_editor', 'Text Editor', editor_component_group() ) | |
->add_fields( 'form', 'Form', form_component_group() ), | |
)) | |
)) | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment