Information for: DEVELOPERS   PARTNERS

Create a custom block

Fast Track to Drupal 8 Coding – Back to intro
Previous lesson - Attach terms to another entity programmatically
Next lesson – Alter an existing simple form

Lesson Goal

Create a custom block named welcome that displays the following content: ‘Welcome to my site’

Implementation Method

Drupal Version Method
Drupal 7 Use hook_block_info to declare a block and hook_block_view to define block content.
Drupal 8 Use Plugins API to declare a block and add a class to define the content.

Drupal 7 method

Add the following code to the lotus.module file:

<?php
/**
* Implements hook_block_info()
**/
function lotus_block_info()
{
  $blocks['welcome'] = array(  'info' => t('Welcome'), );
  return $blocks; }
}
/**
* Implements hook_block_view()
**/
function lotus_block_view($delta = '')
{
  $block = array();    switch ($delta)
  {
      case 'welcome':  $block['subject'] = t('Welcome'); $block['content']
      = array( $block['content'] = 'Welcome to Lotus website!', );
      break;
    }
    return $block;
}

Drupal 8 method

Add the following code to the src/Plugin/Block/HelloBlock.php file:

$this->t('Welcome to Lotus website!'),     );   } }