如何在drupal 8中获取自定义块内容

use*_*914 4 drupal-8

我创建了一个自定义块"admin/structure/block/block-content".

如何通过代码从自定义块中获取字段?

我尝试过block_load功能,entity_load但没有得到预期的结果.

请帮我解决一下.

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

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

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

谢谢

小智 5

你的解决方案几乎是对的.Drupal 8中的自定义块具有不同的实体名称.见下面的例子.

<?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;
  }
}
Run Code Online (Sandbox Code Playgroud)