From b69a81dfb6c5a38c38d12601498c94af1768ca7b Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:58:58 +1000 Subject: [PATCH] More context providing to the block support rather than the Group block --- lib/block-supports/layout.php | 11 +++++++- packages/block-editor/src/hooks/layout.js | 28 +++++++++++++++++++++ packages/block-library/src/group/block.json | 4 --- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php index d7d82d1a113bbb..65406fa1565e14 100644 --- a/lib/block-supports/layout.php +++ b/lib/block-supports/layout.php @@ -6,7 +6,8 @@ */ /** - * Registers the layout block attribute for block types that support it. + * Registers the layout block attribute for block types that support it, + * and sets `provides_context` to allow children to access the layout settings. * * @param WP_Block_Type $block_type Block Type. */ @@ -22,6 +23,14 @@ function gutenberg_register_layout_support( $block_type ) { 'type' => 'object', ); } + + if ( ! isset( $block_type->provides_context ) ) { + $block_type->provides_context = array(); + } + + if ( ! array_key_exists( 'layout', $block_type->provides_context ) ) { + $block_type->provides_context['layout'] = 'layout'; + } } } diff --git a/packages/block-editor/src/hooks/layout.js b/packages/block-editor/src/hooks/layout.js index 586b38bf4c8a0e..65bb8493b0938f 100644 --- a/packages/block-editor/src/hooks/layout.js +++ b/packages/block-editor/src/hooks/layout.js @@ -166,6 +166,29 @@ export function addAttribute( settings ) { return settings; } +/** + * Adds layout to `providesContext` so that the layout context can be accessed + * via child blocks. + * + * @param {Object} settings Original block settings. + * + * @return {Object} Filtered block settings. + */ +export function addProvidesContext( settings ) { + if ( settings.providesContext?.layout ) { + return settings; + } + if ( hasBlockSupport( settings, layoutBlockSupportKey ) ) { + const providesContext = settings.providesContext || {}; + settings.providesContext = { + ...providesContext, + layout: 'layout', + }; + } + + return settings; +} + /** * Override the default edit UI to include layout controls * @@ -240,6 +263,11 @@ addFilter( 'core/layout/addAttribute', addAttribute ); +addFilter( + 'blocks.registerBlockType', + 'core/layout/addProvidesContext', + addProvidesContext +); addFilter( 'editor.BlockListBlock', 'core/editor/layout/with-layout-styles', diff --git a/packages/block-library/src/group/block.json b/packages/block-library/src/group/block.json index 7c282a6941fbe8..2e3a99f78ae3c4 100644 --- a/packages/block-library/src/group/block.json +++ b/packages/block-library/src/group/block.json @@ -17,10 +17,6 @@ "enum": [ "all", "insert", false ] } }, - "providesContext": { - "layout": "layout", - "orientation": "orientation" - }, "supports": { "align": [ "wide", "full" ], "anchor": true,