Creating a Simple Block in Drupal 10
Step 1: Create the Module
Create a new directory in modules/custom
called simple_block
.
Step 2: Define the Module
In the simple_block
directory, create a file named simple_block.info.yml
and add the following:
name: Simple Block
description: A custom module to create a simple block.
type: module
core_version_requirement: ^10
package: Custom
dependencies:
- drupal:block
Step 3: Create the Block Plugin
Create a directory src/Plugin/Block
in your module directory. Inside, create a file named SimpleBlock.php
:
<?php
namespace Drupal\simple_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Simple Block' Block.
*
* @Block(
* id = "simple_block",
* admin_label = @Translation("Simple Block"),
* category = @Translation("Custom")
* )
*/
class SimpleBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => $this->t('Hello, Drupal 10!'),
];
}
}
Step 4: Enable the Module
Enable your new module using Drush or the Drupal admin interface. In your terminal, run:
drush en simple_block
Step 5: Place the Block
Go to Structure > Block layout, find "Simple Block" in the list of blocks, and place it in a region on your site.