Skip to content

Instantly share code, notes, and snippets.

@Xilonz
Created June 21, 2019 11:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Xilonz/3d3cc7db2e5e25ee8611cdfc58d15dba to your computer and use it in GitHub Desktop.
Save Xilonz/3d3cc7db2e5e25ee8611cdfc58d15dba to your computer and use it in GitHub Desktop.
Sage 9 Carbon Field Blocks
<?php
namespace App;
use Carbon_Fields\Field;
use Carbon_Fields\Block;
Block::make( __( 'Gutenberg Block' ) )
->set_icon( 'format-chat')
->add_fields( array(
Field::make( 'rich_text', 'text', __( 'text', 'my-theme' ) )
) )
->set_render_callback( function ( $arg ) {
echo get_block_template('event_comment', $arg);
} );
?>
<?php
namespace App;
function get_block_template( $template, $args ){
$template = App\locate_template([$template.".blade.php", 'resources/views/partials/blocks/'.$template.'.blade.php']);
$data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
return apply_filters("sage/template/{$class}/data", $data, $template);
});
$data = array_merge($data, $args);
if( $template )
echo App\template($template, $data);
else
echo sprintf(__("Template for block %s not found", 'my-theme'), $template);
}
?>
@MrGlox
Copy link

MrGlox commented Aug 27, 2019

Needs to return empty array to avoid array_merge errors

$data = collect(get_body_class())->reduce(function ($data, $class) use ($template) { return apply_filters("sage/template/{$class}/data", $data, $template); }, []);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment