Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import {
getActiveBlockVariation,
getBlockType,
getBlockTypes,
getBlockVariations,
Expand Down Expand Up @@ -1650,10 +1651,26 @@ const isBlockVisibleInTheInserter = (
let current = rootClientId;
let hasParent = false;
do {
if ( parents.includes( getBlockName( state, current ) ) ) {
const ancestorBlockName = getBlockName( state, current );
if ( parents.includes( ancestorBlockName ) ) {
hasParent = true;
break;
}
const ancestorAttributes = getBlockAttributes( state, current );

const variation = getActiveBlockVariation(
ancestorBlockName,
ancestorAttributes,
'inserter'
);

if ( variation ) {
// TODO: Compare block variation name to parent/ancestor list.
// Problem: For template parts that have been persisted to the DB,
// variation.name is prefixed with `instance_` (e.g. `instance_header`).
Comment on lines +1668 to +1670
Copy link
Contributor Author

@ockham ockham Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See

'name' => 'instance_' . sanitize_title( $template_part->slug ),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More context and rationale: #49500

console.log( variation );
}

current = state.blocks.parents.get( current );
} while ( current );

Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export {
getBlockSupport,
hasBlockSupport,
getBlockVariations,
getActiveBlockVariation,
isReusableBlock,
isTemplatePart,
getChildBlockNames,
Expand Down
20 changes: 20 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,26 @@ export const getBlockVariations = ( blockName, scope ) => {
return select( blocksStore ).getBlockVariations( blockName, scope );
};

/**
* Returns the active block variation for a given block based on its attributes.
* Ignored from documentation as the recommended usage is via useSelect from @wordpress/data.
*
* @ignore
*
* @param {string} blockName Name of block (example: “core/columns”).
* @param {Object} attributes Block attributes used to determine active variation.
* @param {WPBlockVariationScope} [scope] Block variation scope name.
*
* @return {(WPBlockVariation|undefined)} Active block variation.
*/
export const getActiveBlockVariation = ( blockName, attributes, scope ) => {
return select( blocksStore ).getActiveBlockVariation(
blockName,
attributes,
scope
);
};

/**
* Registers a new block variation for the given block type.
*
Expand Down
Loading