#! code: Drupal 9: Creating A Block To Render A Node Field

Drupal CMS

Drupal 9 gives you a lot of flexibility to place node fields into different areas of the site, but there are some limitations. You could use the field display manager to change the order and format of the fields, or install the layout builder module and organise fields into sections.

The problem comes when you need to display some fields outside of the limits of the node template. For example, you might want to show a header image within the page template, or print a curated list of links in the sidebar. These situations exist outside of the node template so you need a different way of adding those fields to those parts of the page.

There are just some situations where you just need to render out a field to a different part of the page.

The best way I have found to do this is to use a Drupal block. Since a block can be placed anywhere on the page and can be made aware of the context of the page we can quite easily use a block to pull out the data from the node and print it anywhere we like.

Let’s start with a very basic block implementation that just adds enough information for Drupal to register it as a block.

<?php namespace DrupalmymodulePluginBlock;

use DrupalCoreBlockBlockBase;

* Provides a 'Article Header' block.
* @Block(
* id = "mymodule_article_header",
* label = "Article Header",
* admin_label = @Translation("Article Header"),
* )
class ArticleHeaderBlock extends BlockBase {

* {@inheritdoc}
public function build() {


This block does absolutely nothing, so our next step is to inject a couple of dependencies.

Read more.