How to Create a Custom Block Programmatically in Drupal 8?
1. Create a module To create a custom block, it is necessary to create a “ .info.yml ” file in modules/custom directory. Here a custom directory does not exist. You will need to create it. Create a directory named “ custom ” under the module directory. And under “ modules/custom ” create a directory called “ block_welcome ”. This directory name will be the name of the module created. Inside this folder that you just created, create a “ <module name>.info.yml ” file. Here it will be block_welcome.info.yml as the module name is block_welcome . Within this file, enter the following contents: name: 'Welcome block module' type: module description: 'sample module' core: 8.x package: 'Custom' dependencies: -drupal:block After creating the folder and file with the above content, go to the dashboard and enable the module you just created. The name of the module here is Welcome block module. 2. Create a Drupal Block Class Now to define the logic for the bl...