为每个节点预处理节点类型并在 region.html.twig 模板中打印

Pre-process node type for each node and print within region.html.twig template

我正在尝试弄清楚如何使用预处理检查每个节点的节点 type/content 类型并将其打印在区域模板上 (region.html.twig)

好像要回来了NULL

function iom_preprocess_node(&$variables) {
    $node = $variables["node"];
    $variables['content_type'] = load($node->getType())->label();
}

{{ content_type }}

尝试:

function iom_preprocess_node(&$variables) {
  $node = $variables["node"];
  $variables['content_type'] = $node->bundle();
}

$node->getType() 应该也可以。

对于region.html.twig:

function iom_preprocess_region(&$variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $variables['content_type'] = $node->bundle();
  }
}