Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a custom block programatically in Drupal 8

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 151
    Comment on it

    For making a custom block, the steps which are described below are required:

    Step 1: In your Drupal 8 folder that you have installed you have a folder named module in that module you have to make a sub-folder named custom.

    modules/custom

     

    Step 2: Next step is to create a sub folder in that custom folder that you have recently created.The folder structure goes like this.

     

     

    Step 3: The next step is to create info.yml to make the custom block. In that info.yml, you have to mention the name of the block, description of the block, core, package, dependencies, type.

     

    name: TestArticle Block
    description: A simple module to demonstrate Block.
    core: 8.x
    package: Custom
    dependencies:
    - node
    - block
    type: module

    This file should be saved according to the name that you have created inside the custom folder.

     

    Step 4: After that, you have to create a file with .php extension the name of the file should be same to the name that you have created inside the custom folder.

    <?php
    /**
     * @file
     * Contains \Drupal\article\Plugin\Block\XaiBlock.
     */
    namespace stonesdrupal\testarticle\Plugin\Block;
    use stonesdrupal\Core\Block\BlockBase;
    
    class TestArticleBlock extends BlockBase {
      /**
       * {@inheritdoc}
       */
      public function build() {
        return array(
          '#type' => 'markup',
          '#markup' => 'This block list the article.',
        );
      }
    }

    After that you have to give the permission to the all the folder that you have created. Then your custom block is created.

     

    Step 5: You have to now place the block to the particular region that you want.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: