Skip to main content
Tweeted twitter.com/StackDrupal/status/783638718494244864

How to get a field value of custom block in Drupal 8?

Source Link
asrob
  • 491
  • 1
  • 6
  • 18

How to get a field value of custom block in Drupal 8?

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:

<?php

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;
    }
  }
}