Fields
Adding your own field type
Adding your own field type
A theme or plugin can add a field type without touching GCB Lite. Two halves, both small:
PHP — declare it (on the gcblite_register_control_types action, which
fires on init at 4 — GCB types every block's attributes at 5, so a type
registered later is stored as a string, and you get a _doing_it_wrong):
add_action('init', function () {
wp_register_script('my-timeline-control', plugins_url('timeline.js', __FILE__), ['wp-element', 'wp-components'], '1.0', true);
});
add_action('gcblite_register_control_types', function () {
gcblite_register_control_type('timeline', [
'shape' => 'array', // string|number|integer|boolean|object|array
'doc' => __DIR__ . '/fields/timeline.md', // same frontmatter as schemas/controls/*.md
'script' => 'my-timeline-control', // loaded wherever GCB's field editors are
]);
});JS — the editor component (any script; GCB makes it load after the hub):
window.gcbLiteControls.register( 'timeline', function Timeline( { control, value, onChange, attributes } ) {
// …same props as every GCB control
} );Then "type": "timeline" works in any block.fields.json, in the block
editor, the post-fields meta box and the sidebar panel.
What registering buys: the attribute is typed as shape; the validator knows
the name (BlockGcbValidator::known_types()); and with a doc the type joins
the control vocabulary (ControlDocs::list_types(),
Contract\Fields::control_types()). Built-in names can't be taken, and a hub entry
only replaces a built-in or GCB's own control with { override: true }. The
same entries can come from the gcblite_control_types filter instead.
Contract checks. A field that needs something from the block around it
(a repeater child, a sibling field) can say so with 'check' => callable:
'check' => function (array $control, array $ctx) {
// $ctx: block, controls (this block's), children (what its <Repeater>s allow),
// fields_of(callable: another block's controls)
return $ctx['children'] ? [] : ['a timeline needs a <Repeater> of milestones.'];
},Each message names what to fix. Problems are warned under WP_DEBUG as blocks
load, and returned by the gcblite/check-blocks ability — call it after
creating or changing a block. Lite's own: pin-map (needs a child to place, and
the point field it writes) and layout (needs a repeater to lay out).
The registry in the code
Reference, generated from the plugin source
gcblite_register_control_type( string $type, array $args ): boolshape is one of string, number, integer, boolean, object, array. The type name must be lowercase letters, digits and dashes, and not a name GCB ships. Register on the gcblite_register_control_types action, which fires on init at priority 4.
window.gcbLiteControls
Where a field type's editor component is registered, for every GCB field editor on the page (the block editor, the post-fields meta box and the sidebar panel). Loaded by the gcblite-control-hub script.
| Method | Description |
|---|---|
register( type, component, options = {} ) | Params: (string) type the field type, as in block.fields.json; (Function|Object) component a React component (function, memo or forwardRef); (Object) [options]; (boolean) [options.override] replace a built-in / GCB control of the same name |
if( ! ok ) | |
get( type ) | The component registered for a type, or undefined. |
types( ) | Every type registered here. |
subscribe( fn ) | Hear about every registration — the ones already made, then each new one. Params: (Function) fn ( type, { component, override } ) |
if( i !== -1 ) |