Blocks and Block Cache
Blocks are one of the fundamental building elements in Drupal. They allow site builders and developers to place reusable pieces of content or functionality across different regions of a website. From displaying menus and banners to embedding custom logic, blocks provide flexibility to structure and enhance your site’s layout. In this blog, we’ll explore how to create blocks in Drupal and dive into the concept of block caching.
What is a Block in Drupal?
A block in Drupal is a container of content or functionality that can be placed in specific regions of a theme (like sidebar, footer, header). They can be:
Core blocks: Predefined blocks provided by Drupal (e.g., "User login", "Search form").
Custom blocks: Created by site administrators or developers with custom content or logic.
Contributed module blocks: Blocks exposed by installed contributed modules.
Creating Blocks in Drupal
1. Creating a Block via the UI
Go to Structure > Block layout.
Click Add custom block.
Enter a title and block body.
Save the block.
Assign it to a region in your theme (like sidebar first, footer, etc.) via the block layout page.
This is useful for simple static content blocks.
2. Creating a Custom Block in Code (Drupal 9/10)
You can create blocks with custom logic using a Block Plugin.
Example: Hello World Block
Step 1: Define the Block Plugin
Create a file src/Plugin/Block/HelloWorldBlock.php
inside your custom module:
<?php
namespace Drupal\custom_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello World' block.
*
* @Block(
* id = "hello_world_block",
* admin_label = @Translation("Hello World Block"),
* category = @Translation("Custom")
* )
*/
class HelloWorldBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => $this->t('Hello, World! This is a custom block.'),
];
}
}
Step 2: Clear the Cache
Run:
drush cr
Step 3: Place the Block
Go to Structure > Block layout.
Find your theme’s region and place the Hello World Block.
What is Block Cache in Drupal?
Drupal uses caching to improve performance. Rendering blocks for every request can be expensive, especially if they fetch data from databases or APIs. This is where block cache comes in.
Cache per role/user: Some blocks can be cached differently for different user roles or authenticated/anonymous users.
Cache contexts: Define under what conditions the block output varies (e.g., by URL, user role, language).
Cache tags: Define dependencies so the block cache is invalidated when related content changes (e.g., a block showing a specific node will be cleared if that node updates).
Cache max-age: Determines how long a block’s output can remain cached (e.g., unlimited, 1 hour, or no caching).
Example: Adding Cache Metadata to a Block
public function build() {
return [
'#markup' => $this->t('Dynamic block content.'),
'#cache' => [
'contexts' => ['user.roles'],
'tags' => ['node:1'],
'max-age' => 3600,
],
];
}
This means:
The block changes depending on the user role.
If Node 1 is updated, the block cache is invalidated.
The block cache is valid for 1 hour (3600 seconds).
Why Block Cache is Important?
Improves performance by reducing repeated rendering.
Ensures dynamic content is shown correctly to the right users.
Helps with scalability, especially in large Drupal sites with many visitors.
Blocks in Drupal are a powerful way to add and manage content in structured layouts. With the ability to create them via UI or programmatically, they provide great flexibility. Understanding how block caching works is equally important to ensure your site remains fast, efficient, and serves the right content at the right time.