I have a custom block type, called "Features block". I added a field as alternative title (field_block_alt_title is that machine name).

I would like to display this alternative title on frontpage programmatically.

Now, I have this code so far that is displaying the title of blocks:


use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element;

function neft_preprocess_region(&$variables) {
  if ($variables['region'] == 'flick') {
    $titles = array();
    $ids = array();
    $block_ids = Element::children($variables['elements']);
    foreach ($block_ids as $block_id) {
      $block_title = Drupal\block\Entity\Block::load($block_id)->label();
      $titles[] =  Html::escape($block_title);
      $ids[] = 'block-' . Html::getUniqueId($block_id);
    if (count($titles) && count($ids)) {
      $tabs = '<ul>';
      foreach ($titles as $key => $title) {
        $tabs .= "<li><a href=\"#$ids[$key]\">$title</a></li>";
      $tabs .= '</ul>';
      $variables['tabs'] = $tabs;

Fields of custom blocks are in block content. So you have to load the block content first, before you can get the field value:

$block = \Drupal\block\Entity\Block::load($block_id);
$uuid = $block->getPlugin()->getDerivativeId();
$block_content = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
if ($block_content) {
  $field_value = $block_content->field_block_alt_title->value;

I was trying to do the same today and this is the only one that worked for me on the latest Drupal version (8.6.12).

use \Drupal\block_content\BlockContentInterface;

function HOOK_preprocess_block(&$variables)

    $content = $variables['elements']['content'];
    if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) {
        $blockType = $content['#block_content']->bundle();

        if ($blockType === 'CUSTOM_BLOCK_TYPE') {
            $variables['FIELD_VALUE_ACCESSIBLE_VIA_TEMPLATE'] = $content['#block_content']->get('FIELD_NAME')->value;


Then in your template file.


You can load the field like:

$alt_title = Drupal\block\Entity\Block::load($block_id)->get('field_block_alt_title')->value;


$alt_title = Drupal\block\Entity\Block::load($block_id)->field_block_alt_title->value;

You need to expose the fields from Node to Block variables.

function MYTHEME_preprocess_block(&$variables) {
        $node = $variables['elements']['content']['#view']->result[0]->_entity;

      if ($node instanceof \Drupal\node\Entity\Node) {

        if ($node->hasField('field_name_1')) {
          $variables['field_name_1'] = $node->get('field_name_1')->value;

        if ($node->hasField('field_name_2')) {
          $variables['field_name_2'] = $node->get('field_name_2')->value;


To use in your block--views-block--block-custom.html.twig



Tested on Drupal 8.5.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.