Skip to content

Commit f733d8f

Browse files
committed
Add data type template as a block
1 parent 2d6d93a commit f733d8f

4 files changed

+68
-3
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
### TODO
66

7-
- [ ] Default pattern for created CPT (i.e., render a default single.html content, also save a pattern)
87
- [ ] Document stuff better and show teammates
98
- [ ] Lock all content and styling except bound blocks's inside when entering data
109
- [ ] Work on examples that have a primary and secondary content

inc/data-type-management.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ function _get_custom_fields( $blocks ) {
100100
foreach ( $blocks as $block ) {
101101
$binding = $block['attrs']['metadata']['data-types/binding'] ?? null;
102102

103-
if ( is_null( $binding ) || 'post_content' === $binding ) {
103+
if ( 'post_content' === $binding ) {
104104
continue;
105105
}
106106

107-
$acc[] = $binding;
107+
if ( ! is_null( $binding ) ) {
108+
$acc[] = $binding;
109+
}
108110

109111
if ( ! empty( $block['innerBlocks'] ) ) {
110112
$acc = array_merge( $acc, _get_custom_fields( $block['innerBlocks'] ) );

inc/templating.php

+47
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,50 @@ function register_field_categories( $categories ) {
6060

6161
return $categories;
6262
}
63+
64+
add_action( 'init', 'register_data_type_template_block' );
65+
66+
/**
67+
* Register a block that represents the data type template.
68+
*/
69+
function register_data_type_template_block() {
70+
$block_name = 'data-types/type-template';
71+
72+
$args = array(
73+
'api_version' => 1,
74+
'title' => 'Data type template',
75+
'attributes' => array(),
76+
// translators: %s is the data type's name.
77+
'description' => __( 'Template for the data type.' ),
78+
'category' => 'text',
79+
'render_callback' => function () {
80+
global $post;
81+
82+
if ( ! $post ) {
83+
return __( 'No post found' );
84+
}
85+
86+
$template = get_data_type_template( $post->post_type );
87+
88+
$parsed_template = parse_blocks( $template );
89+
$hydrated_blocks = hydrate_blocks_with_content( $parsed_template );
90+
91+
return implode( '', array_map( 'render_block', $hydrated_blocks ) );
92+
},
93+
);
94+
95+
register_block_type( $block_name, $args );
96+
}
97+
98+
add_action(
99+
'enqueue_block_editor_assets',
100+
function () {
101+
wp_enqueue_script(
102+
'data-types/type-template',
103+
plugin_dir_url( __FILE__ ) . '/type-template-inserter.js',
104+
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post', 'wp-i18n' ),
105+
'v1',
106+
true
107+
);
108+
}
109+
);

inc/type-template-inserter.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function typeTemplateInserter( wp ) {
2+
const el = wp.element.createElement;
3+
4+
wp.blocks.registerBlockType( 'data-types/type-template', {
5+
title: wp.i18n.__( 'Data type template' ),
6+
edit( props ) {
7+
return el( wp.editor.ServerSideRender, {
8+
block: 'data-types/type-template',
9+
attributes: props.attributes,
10+
} );
11+
},
12+
13+
save: () => null,
14+
} );
15+
}
16+
17+
setTimeout( typeTemplateInserter, 800, window.wp );

0 commit comments

Comments
 (0)