代码之家  ›  专栏  ›  技术社区  ›  clestcruz

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

  •  1
  • clestcruz  · 技术社区  · 7 年前

    (region.html.twig)

    好像又回来了 NULL

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

    {{ content_type }}

    1 回复  |  直到 7 年前
        1
  •  1
  •   GregCube    7 年前

    尝试:

    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();
      }
    }
    
    推荐文章