From cd2dcdcff7f714422a8693074cf88972dd5dc858 Mon Sep 17 00:00:00 2001 From: Joe Simpson Date: Tue, 7 Dec 2021 22:37:13 +0000 Subject: [PATCH] Add align option to Gutenberg blocks --- core/Container/Block_Container.php | 18 ++++++++++++++++++ packages/blocks/index.js | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/Container/Block_Container.php b/core/Container/Block_Container.php index 23762ea58..84b3a8711 100644 --- a/core/Container/Block_Container.php +++ b/core/Container/Block_Container.php @@ -22,6 +22,8 @@ class Block_Container extends Container { 'template_lock' => null, 'allowed_blocks' => null, ), + 'align' => false, + 'align_default' => false, 'category' => array( 'slug' => 'common', ), @@ -304,6 +306,22 @@ public function set_inner_blocks( $inner_blocks = true ) { return $this; } + /** + * Set the align options available for this block. + * + * Either pass a boolean or an array of values `left`, `center`, `right`, `wide`, and `full`. + * + * @param array|boolean $inner_blocks + * @param string|boolean $default + * @return Block_Container + */ + public function set_align( $align = false, $default = false ) { + $this->settings[ 'align' ] = $align; + $this->settings[ 'align_default' ] = $default; + + return $this; + } + /** * Set the position of the inner blocks to be rendered * above or below the fields. diff --git a/packages/blocks/index.js b/packages/blocks/index.js index 583df01fc..298ea24d6 100644 --- a/packages/blocks/index.js +++ b/packages/blocks/index.js @@ -41,6 +41,16 @@ get( window.cf, 'preloaded.blocks', [] ).forEach( ( container ) => { containerDefinitions[ name ] = container; fieldDefinitions[ name ] = container.fields.map( ( field ) => ( { ...field } ) ); + let attributes = {}; + + let alignDefault = getBlockSetting( 'align_default', false ); + if(alignDefault) { + attributes['align'] = { + 'type': 'string', + 'default': alignDefault + }; + } + registerBlockType( `carbon-fields/${ name }`, { title: container.title, icon: getBlockSetting( 'icon' ), @@ -52,13 +62,14 @@ get( window.cf, 'preloaded.blocks', [] ).forEach( ( container ) => { data: { type: 'object', default: fields - } + }, + ...attributes }, supports: { tabs: isPlainObject( getBlockSetting( 'tabs' ) ), preview: getBlockSetting( 'preview' ), innerBlocks: getBlockSetting( 'inner_blocks.enabled' ), - alignWide: false, + align: getBlockSetting( 'align', false ), anchor: false, html: false },