From 1fde7653af279ad1f4c0209d6fe10b35cc835bf0 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 2 May 2023 16:18:57 +0800 Subject: [PATCH] Fix template part area variation matching (#49500) * Use a different name for instance and area template part variations to ensure they can all be registered * Hide area variations in inserter when an instance exists for the same area --- .../block-library/src/template-part/index.php | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 54dc3f1b1179c..e12f67566a0fd 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -173,21 +173,34 @@ function render_block_core_template_part( $attributes ) { /** * Returns an array of area variation objects for the template part block. * + * @param array $instance_variations The variations for instances. + * * @return array Array containing the block variation objects. */ -function build_template_part_block_area_variations() { +function build_template_part_block_area_variations( $instance_variations ) { $variations = array(); $defined_areas = get_allowed_block_template_part_areas(); + foreach ( $defined_areas as $area ) { if ( 'uncategorized' !== $area['area'] ) { + $has_instance_for_area = false; + foreach ( $instance_variations as $variation ) { + if ( $variation['attributes']['area'] === $area['area'] ) { + $has_instance_for_area = true; + break; + } + } + + $scope = $has_instance_for_area ? array() : array( 'inserter' ); + $variations[] = array( - 'name' => $area['area'], + 'name' => 'area_' . $area['area'], 'title' => $area['label'], 'description' => $area['description'], 'attributes' => array( 'area' => $area['area'], ), - 'scope' => array( 'inserter' ), + 'scope' => $scope, 'icon' => $area['icon'], ); } @@ -223,7 +236,7 @@ function build_template_part_block_instance_variations() { foreach ( $template_parts as $template_part ) { $variations[] = array( - 'name' => sanitize_title( $template_part->slug ), + 'name' => 'instance_' . sanitize_title( $template_part->slug ), 'title' => $template_part->title, // If there's no description for the template part don't show the // block description. This is a bit hacky, but prevent the fallback @@ -255,7 +268,9 @@ function build_template_part_block_instance_variations() { * @return array Array containing the block variation objects. */ function build_template_part_block_variations() { - return array_merge( build_template_part_block_area_variations(), build_template_part_block_instance_variations() ); + $instance_variations = build_template_part_block_instance_variations(); + $area_variations = build_template_part_block_area_variations( $instance_variations ); + return array_merge( $area_variations, $instance_variations ); } /**