Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Develop Custom Block in Drupal 8 Programmatically - 2 Steps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 755
    Comment on it

    Drupal has innovative feature to create a block. Blocks are the box of content in which we can wrap the content. Drupal has structure of regions and blocks in which we can assign the blocks into a particular region we have created. Drupal support two type of block one is created using drupal admin UI and second created custom programmatically. In this tutorial, we will be discussing about createing block programmatically.

     

     

     

    Basically we use some drupal core block API to develop a custom block Like Block Plugin API, Entity API that will be used for block placement and visibility control.

    We need the following steps to create custom block:

    1: Create a block using annotation.

    2: Extend the Drupal\Core\Block\Block base class.

     

    The unique, machine readable ID of your block gets its definition from 'id' property in the annotation
     
    As far as the 'admin_label' annotation is concerned, it serves to define the human readable name of the block that is meant to display your block in the admin interface.
     
     Scanning of your module's classes for a class, containing the @Block Annotation (a special comment right above your class declaration), is done via  Drupal block manager .

    namespace Drupal\test\Plugin\Block;
    
    use Drupal\Core\Block\BlockBase;
    
    /**
     * Provides a 'Test' block.
     *
     * @Block(
     *   id = "test_block",
     *   admin_label = @Translation("Test block"),
     * )
     */
    class TestBlock extends BlockBase {
      // Override BlockPluginInterface methods here.
    }

     

    We need your custom and contributed module in root directory:

    modules/custom

    modules/contrib

    Step 1: Create a file inside of your block blockName.info.yml. It contains the metadata of your block modules.

    name: Test Article 
    description: 'It will show content of your article'
    type: module
    core: 8.x
    package: Custom

     

    Step 2: Create a file inner your path  test/src/plugin/Block/TestBlock.php

    <?php
    /**
     * @file
     * Contains \Drupal\test\Plugin\Block\XaiBlock.
     */
    namespace Drupal\test\Plugin\Block;
    use Drupal\Core\Block\BlockBase;
    /**
     * Provides a 'Test' Block
     * @Block(
     *   id = "article_block",
     *   admin_label = @Translation("Test block"),
     * )
     */
    class TestBlock extends BlockBase {
      /**
       * {@inheritdoc}
       */
      public function build() {
        return array(
          '#type' => 'markup',
          '#markup' => 'This block list the article.',
        );
      }

    Now you have done the necessary things. After this, drupal block manager will start reading your block and show it in your search block list. And you can also place your block in particular region.

 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: