Create a custom block

Fast Track to Drupal 8 Coding`Back to Intro </tutorials/fast-track-drupal-8-coding>`__
Previous lesson – `Attach terms to another entity programmatically </tutorials/fast-track-drupal-8-coding/attach-terms-another-entity-programmatically>`__  |  Next lesson – `Alter an existing simple form </tutorials/fast-track-drupal-8-coding/alter-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:

/** * 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!'),     );   } }

Contact supportStill need assistance? Contact Acquia Support

Acquia: Think Ahead

53 State Street, 10th Floor
Boston, MA 02109
United States
Phone: 888-922-7842

Map: Google Maps
View other locations