Top menu

Drupal 9 Create Block programmatically and insert it in template file

Create the block class file /web/modules/custom/my_module/src/Plugin/Block/HelloBlock.php and add the code below. To insert this block programmatically in a template we can implement a hook_preprocess into .module file of our module

Then into our template file we display the variable

don’t forget to execute : vendor/bin/drush cr