How to get user block contents in drupal 8

I created a custom block "admin/structure/block/block-content. "

How to get a field from a custom block by code?

I tried with the function block_loadand entity_load, but did not get the expected result.

Please help me solve the problem.

$ block = \ Drupal :: entityManager () → getStorage ('block') → load ($ block_id);

$ block_view = \ Drupal :: entityManager () → getViewBuilder ('block') → view ($ block);

http://i.stack.imgur.com/fOuSW.png

thank

+4
source share
1 answer

Your decision is almost correct. Custom blocks in Drupal 8 have a different object name. See the example below.

<?php

/**
 * Implements hook_preprocess_html().
 */
function my_module_preprocess_html(&$variables) {
  // You can do some logic like showing your custom block on certain pages or
  // under certain conditions.
  if (\Drupal::routeMatch()->getRouteName() == 'some.path') {
    $block = \Drupal::entityManager()->getStorage('block_content')->load(1);
    $block_view = \Drupal::entityManager()->getViewBuilder('block_content')->view($block);
    $variables['page']['sidebar_first']['custom_block'] = $block_view;
  }
}
+5
source

Source: https://habr.com/ru/post/1548320/


All Articles