WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.52k stars 4.21k forks source link

Using the 'allowed_block_types_all' hook, it is not possible to search from the Block Directory. #33954

Open ddryo opened 3 years ago

ddryo commented 3 years ago

Is there an existing issue for this?

Have you tried deactivating all plugins except Gutenberg?

Have you tried replicating the bug using a default theme e.g. Twenty Twenty?

Description

Using the 'allowed_block_types_all' hook will prevent searching from the Block Directory when searching for blocks.

The reason we are using the 'allowed_block_types_all' hook is that we want to turn off the block for full site editing, which was added in 5.8.

If it is difficult to solve this problem, I would like to see a hook added that allows you to specify 'disallowed blocks' instead of 'allowed blocks'.

Translated with www.DeepL.com/Translator (free version)

Step-by-step reproduction instructions

  1. Use the 'allowed_block_types_all' hook to restrict the block. For example, the following
add_filter( 'allowed_block_types_all', 'test__limit_block_types', 99, 2 );
function test__limit_block_types( $allowed_block_types, $block_editor_context ) {

    // 全ブロック取得
    $all_blocks  = [];
    $block_types = \WP_Block_Type_Registry::get_instance()->get_all_registered();
    foreach ( $block_types as $block_type ) {
        $all_blocks[] = $block_type->name;
    }

    $FSE_blocks = [
        'core/loginout',
        'core/page-list',
        'core/post-content',
        'core/post-date',
        'core/post-excerpt',
        'core/post-featured-image',
        'core/post-terms',
        'core/post-title',
        'core/post-template',
        'core/query-loop',
        'core/query',
        'core/query-pagination',
        'core/query-pagination-next',
        'core/query-pagination-numbers',
        'core/query-pagination-previous',
        'core/query-title',
        'core/site-logo',
        'core/site-title',
        'core/site-tagline',
    ];

    $allowed_blocks = array_diff( $all_blocks, $FSE_blocks );
    $allowed_blocks = array_values( $allowed_blocks );
    return $allowed_blocks;
}
  1. Search for blocks. For example, "countdown".

Expected Behavior

スクリーンショット 2021-08-09 22 47 17

Current Behavior

スクリーンショット 2021-08-09 22 37 05

Screenshots or screen recording (optional)

No response

Code snippet (optional)

No response

WordPress Information

No response

Gutenberg Information

No response

What browsers are you seeing the problem on?

No response

Device Information

No response

Operating System Information

No response

annezazu commented 3 years ago

Hey there! Thanks for opening this issue. In case it's helpful, you might find this plugin easier to use to manage various blocks in the meantime globally: https://wordpress.org/plugins/block-manager/ You could also use the built in Block Manager to hide these blocks.

ddryo commented 3 years ago

@annezazu

Thank you very much. However, I want to force the FSE block to be turned off on a theme I sell, not on my own site. So, installing a plugin is not a desirable solution.

The only way to do this is to use the 'allowed_block_types_all' hook. Is there any good way to avoid using this hook?

draganescu commented 3 years ago

I have stubmled upon this on my blog as well using the Go theme

image

even though the search in the block directory happens as shown by the console:

https://.../wp-json/wp/v2/block-directory/search?term=team&_locale=user shows:

[{
    "name": "block\/team-member",
    "title": "Team Member",
    "description": "Present your team members beautifully & gain instant credibility.",
    "id": "team-member-block",
    ...
    }
}, {
    "name": "straightvisions\/twitch-stream",
    ...
}]
annezazu commented 3 years ago

Ah ha thank you for clarifying @ddryo - I'm going to leave this issue open as this seems to be something to address. cc @ryelle in case she has thoughts as someone who worked on the block directory implementation.

ryelle commented 3 years ago

This isn't inherently a problem with the block directory - the issue is that when you set the allowed blocks list to a list of blocks, it's assumed that you only want those blocks so anything that doesn't match, including from the block directory, is filtered out.

If it is difficult to solve this problem, I would like to see a hook added that allows you to specify 'disallowed blocks' instead of 'allowed blocks'.

I agree that this would be a better solution for disabling specific blocks.

ddryo commented 3 years ago

The following code solved the problem I am personally having.

import { addFilter } from '@wordpress/hooks';
addFilter('blocks.registerBlockType', 'my-theme/filter-blocks', function (settings, name) {
    const fseBlocks = [
        'core/loginout',
        'core/page-list',
        'core/post-content',
        'core/post-date',
        'core/post-excerpt',
        'core/post-featured-image',
        'core/post-terms',
        'core/post-title',
        'core/post-template',
        'core/query-loop',
        'core/query',
        'core/query-pagination',
        'core/query-pagination-next',
        'core/query-pagination-numbers',
        'core/query-pagination-previous',
        'core/query-title',
        'core/site-logo',
        'core/site-title',
        'core/site-tagline',
    ];
    if (-1 !== fseBlocks.indexOf(name)) {
        return {};
    }
    return settings;
});

By changing to the above code instead of using 'allowed_block_types_all', I was able to successfully turn off certain blocks but keep the search from the block directory enabled.

However, I think it would be useful to have a hook to specify the "disallowed blocks", so I will leave the issue open.