4

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

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

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.

{{ FIELD_VALUE_ACCESSIBLE_VIA_TEMPLATE }}
2

You can load the field like:

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

or

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

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

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

    if($node){
      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

<div>
     {{field_name_1}}       
</div>

<div>
     {{field_name_2}}       
</div>

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.