Skip to content

Commit

Permalink
Fix template part area variation matching (#49500)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
talldan authored May 2, 2023
1 parent 016651b commit 1fde765
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

/**
Expand Down

0 comments on commit 1fde765

Please sign in to comment.