【问题标题】:How to customize/style/theme the output of a custom content type in Drupal 8?如何在 Drupal 8 中自定义/样式/主题自定义内容类型的输出?
【发布时间】:2019-03-23 02:50:51
【问题描述】:

我正在创建自己的 Drupal 8 “经典”子主题。现在我用一些文本字段创建了一个名为“s-box”的内容类型。我还创建了一个名为“scol”的视图,它是一个“s-box”列表。这个“scol”作为一个块添加到站点的侧边栏中。

效果很好(这意味着所有内容都已显示),但我希望某些内容类型字段嵌套在特定标记中(如 h4 中的标题)。最好,我想为此内容类型“s-box”自定义整个输出。

但我想不通,树枝应该叫什么名字。到目前为止,我尝试过:

node--s-box.html.twig (what I thought would be the correct name)  
block--scol.html.twig  
field--node--s-box.html.twig  
field--s-box.html.twig  
node--scol.html.twig  
scol--s-box.html.twig

完美的解决方案是一个模板,我可以在其中构建自己的 html 并分配内容类型的字段。

【问题讨论】:

    标签: drupal drupal-8 drupal-content-types


    【解决方案1】:

    只需启用 Twig 调试。见Debugging Twig templates。启用 Twig Debugging 后,您将获得作为 HTML cmets 直接打印到您的标记中的模板命名建议。在那里你会看到这个模板应该如何被准确命名。


    您的sites/development.services.yml 应该如下所示:

    # Local development services.
    #
    # To activate this feature, follow the instructions at the top of the
    # 'example.settings.local.php' file, which sits next to this file.
    parameters:
      http.response.debug_cacheability_headers: true
      twig.config:
          debug: true
          auto_reload: true
          cache: false
    services:
      cache.backend.null:
        class: Drupal\Core\Cache\NullBackendFactory
    

    您需要将以下行添加到您的settings.phpsettings.local.php

    /**
     * Enable local development services.
     */
    $settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
    

    最终会给你类似的东西:

    <!-- FILE NAME SUGGESTIONS:
       * node--page--j1.html.twig
       * node--1--full.html.twig
       * node--1.html.twig
       * node--page--full.html.twig
       * node--page.html.twig
       * node--full.html.twig
       x node.html.twig
    -->
    

    别忘了清除缓存!多次。

    【讨论】:

    • 谢谢!我已经启用了此功能,但现在您再次指出这一点,我找到了解决方案。我没有注意到我可以访问“views-view-fields.html.twig”中的所有字段。
    【解决方案2】:

    解决办法是:

    由于我的内容类型在视图中,我可以访问 views-view-fields--[my-view].html.twig 中的所有字段(在我的情况下:views-view-fields--scol.html.twig

    键是fields 数组。我可以通过机器名称访问所有字段。喜欢fields.title.contentfields.field_foo.content

    因此我可以在此处设置 s-box 的整个输出样式。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多