Create a custom block

Tutorial Goal

Lesson Goal:

Implementation Method

D7

D8

hook_block_info() to declare a block and hook_block_view() to define block content

Use Plugins API to declare a block and add a class to define the content.

D7 Code

Add the following in 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;
}

D8 Code

Add the following in src/Plugin/Block/HelloBlock.php file

<?php

namespace Drupal\lotus\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Hello' Block
 *
 * @Block(
 *   id = "hello_block",
 *   admin_label = @Translation("Hello block"),
 * )
 */
class HelloBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    return array(
      '#markup' => $this->t('Welcome to Lotus website!'),
    );
  }
}

Gist Link

https://gist.github.com/prasadshir/0bbba050e9f5cd16591822f9d143702c

Contact supportStill need assistance? Contact Acquia Support