@larissa
To create a custom block in Drupal 8, you can use one of the following methods:
1
|
drupal generate:plugin:block |
Here is an example of a custom block class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace DrupalmymodulePluginBlock; use DrupalCoreBlockBlockBase; use DrupalCoreBlockBlockPluginInterface; use DrupalCoreFormFormStateInterface; /** * Provides a 'MyCustomBlock' block. * * @Block( * id = "mycustom_block", * admin_label = @Translation("My custom block"), * category = @Translation("Custom"), * ) */ class MyCustomBlock extends BlockBase implements BlockPluginInterface { /** * {@inheritdoc} */ public function build() { $build = []; $build['mycustom_block']['#markup'] = 'Hello World!'; return $build; } } |
I hope this helps! Let me know if you have any further questions.
@larissa
To create a custom block in Drupal 8, you need to follow these steps:
1 2 3 4 5 6 7 |
name: 'Custom Block Module' type: module description: 'A custom module to create a custom block.' core_version_requirement: ^8 || ^9 package: Custom dependencies: - block |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
namespace Drupalmodule_namePluginBlock; use DrupalCoreBlockBlockBase; /** * Provides a custom block. * * @Block( * id = "custom_block", * admin_label = @Translation("Custom Block"), * category = @Translation("Custom") * ) */ class CustomBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { // Build the content of the block. // You can use any HTML or render arrays here to define the block's output. return [ '#markup' => $this->t('This is a custom block.'), ]; } } |
That's it! You have successfully created a custom block in Drupal 8.