---
title: "Drupal 8: Creating a configurable block · Appnovation"
url: "https://www.appnovation.com/insights/drupal-8-creating-configurable-block"
description: "Step-by-step tutorial: build a configurable Drupal 8 block plugin that takes a name as input and prints a custom hello message."
---

Article

# Drupal 8: Creating a configurable block

![Drupal 8: Creating a configurable block](https://cdn.sanity.io/images/ey850001/production/9efe1c26d9584ecfb3dfef5a63e698bf4567d305-1440x600.jpg?w=1400&auto=format) 

Drupal 8 blocks are implemented as plugins. Plugins are basically "small pieces of functionality that are swappable" (https://www.drupal.org/developing/api/8/plugins).

We'll create a simple Hello block that takes in name and simply prints it out.

First we'll create a module called **Hello** to store this block. To do that, we simply create a .info.yml file like so: **/modules/custom/hello/hello.info.yml**

name: Hello type: module description: Hello module core: 8.x 

Next we'll create the block. Create the following file **/modules/custom/hello/src/Plugin/Block/HelloBlock.php**

\<?php namespace Drupal\\hello\\Plugin\\Block; use Drupal\\Core\\Block\\BlockBase; /\*\* \* Provides a 'HelloBlock' block. \* \* @Block( \* id = "hello\_block", \* admin\_label = @Translation("Hello block"), \* ) \*/ class HelloBlock extends BlockBase { /\*\* \* {@inheritdoc} \*/ public function build() { $build = \[\]; $build\['hello\_block'\]\['#markup'\] = 'Hello World.'; return $build; } } 

A few things to note here.

\* @Block( \* id = "hello\_block", \* admin\_label = @Translation("Hello block"), \* )

is required because that is how Drupal will discover your block. It's equivalent to hook\_block\_info in Drupal 7.

We're also extending the **BlockBase** class. This provides us with useful functionalities for configuring our block.

Right now, our block simply prints out the text 'Hello World'. We want to make this block be configurable, so let's override **BlockBase**'s blockForm() and blockSubmit() methods.

/\*\* \* {@inheritdoc} \*/ public function blockForm($form, FormStateInterface $form\_state) { $form\['name'\] = array( '#type' => 'textfield', '#title' => $this->t('Name'), '#description' => $this->t('Your name'), '#default\_value' => isset($this->configuration\['name'\]) ? $this->configuration\['name'\] : 'John Doe', '#maxlength' => 64, '#size' => 64, '#weight' => '0', ); return $form; } /\*\* \* {@inheritdoc} \*/ public function blockSubmit($form, FormStateInterface $form\_state) { $this->configuration\['name'\] = $form\_state->getValue('name'); }

This allows us to configure the block and enter in our custom name. To display our name, we simply modify the **build()** to

/\*\* \* {@inheritdoc} \*/ public function build() { $build = \[\]; $build\['hello\_block'\]\['#markup'\] = '\<p>' . $this->configuration\['name'\] . '\</p>'; return $build; }

Now simply enable our **Hello** module and use the block administration interface to place our block in any region.

## Keep reading

[![Got Drupal? What you need to know about maintainability, quality assurance and automation](https://cdn.sanity.io/images/ey850001/production/7fbfb2113f03477b14689f7932ee0232a12c36fc-1440x600.jpg?w=640&auto=format)](/insights/2020-02-got-drupal-what-you-need-know-about-maintainability-quality-assurance-automation.md)

 Article · 6 min Feb 13, 2020 

### [Got Drupal? What You Need to Know About Maintainability, Quality Assurance & Automation](/insights/2020-02-got-drupal-what-you-need-know-about-maintainability-quality-assurance-automation.md)

Drupal and Drupal 8 users and web developers get tips on content management system maintainability, quality assurance and automation from managed service provider and digital consultancy Appnovation.

[Danielle Edwards](/people/danielle-edwards.md) Article 

[![SEO: The Google Algorithm and The Knowledge Graph](https://cdn.sanity.io/images/ey850001/production/73475661cdff04c3d07d18a4e114374c75f961af-1886x600.jpg?w=640&auto=format)](/insights/seo-google-algorithm-and-knowledge-graph.md)

 Article · 13 min Feb 28, 2019 

### [SEO: The Google Algorithm and The Knowledge Graph](/insights/seo-google-algorithm-and-knowledge-graph.md)

[tkirby tkirby](/people/tkirby-tkirby.md) Article 

[![Acquia Engage Wrap-Up & Your Digital Strategy Roadmap for 2019](https://cdn.sanity.io/images/ey850001/production/377391b255aa67ee00f42cb8cc8bd7e221b3e512-1600x900.jpg?w=640&auto=format)](/insights/acquia-engage-wrap-your-digital-strategy-roadmap-2019.md)

 Article · 3 min Nov 19, 2018 

### [Acquia Engage Wrap-Up & Your Digital Strategy Roadmap for 2019](/insights/acquia-engage-wrap-your-digital-strategy-roadmap-2019.md)

[Kate Kim](/people/kate-kim.md) Article 

## Get the thinking,  
*before everyone else.*

One considered email a month. New essays, field notes from live engagements, and the occasional strong opinion. No noise.
