By longwave on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
11.5.x
Introduced in version:
11.5.0
Issue links:
Description:
The allow_in_navigation property marks a block as available for placement in the navigation. Setting it previously required an implementation of hook_block_alter().
Blocks can now declare support with the #[AllowInNavigation] attribute. The property is set during block discovery when the navigation module is installed. The module that provides the block does not need to depend on navigation.
Before
#[Hook('block_alter')]
public function blockAlter(&$definitions): void {
$definitions['my_block']['allow_in_navigation'] = TRUE;
}
After
use Drupal\navigation\Attribute\AllowInNavigation;
#[Block(
id: 'my_block',
admin_label: new TranslatableMarkup('My block'),
)]
#[AllowInNavigation]
final class MyBlock extends BlockBase {}
hook_block_alter() can still be used for backward compatibility, including to alter other blocks that you do not own.
Using the attribute on a plugin that is not a block throws InvalidPluginDefinitionException.
Impacts:
Module developers