logo

额外区块类型 (EBT) - 全新的布局构建器体验❗

额外区块类型 (EBT) - 样式化、可定制的区块类型:幻灯片、标签页、卡片、手风琴等更多类型。内置背景、DOM Box、JavaScript 插件的设置。立即体验布局构建的未来。

演示 EBT 模块 下载 EBT 模块

❗额外段落类型 (EPT) - 全新的 Paragraphs 体验

额外段落类型 (EPT) - 类似的基于 Paragraph 的模块集合。

演示 EPT 模块 滚动

滚动

在 Drupal 中创建自定义内容类型

30/09/2025, by Ivan

Эта страница является копией конфигурации «Включить по умолчанию» в модуле Drupal 8. Это следует рассматривать как已弃用。

在 Drupal 8 中,借助新的配置 API,创建自定义内容类型变得相当简单。

前提条件

  • 已安装 Drupal 8.0.x
  • 有一个自定义模块(在本示例中使用的模块名为 foobar)

创建自定义内容类型

如介绍中所述,创建自定义内容类型是通过创建几个 YAML 文件完成的,这些文件包含所有必需的设置。在此示例中,我们将创建一个名为 Car Brand 的内容类型,它将包含两个字段(默认):正文和标题。

foobar/config/install/node.type.car_brand.yml

此文件将告诉 Drupal 它应创建一个新的内容类型。

注意: 请记住,我们添加了对 foobar 模块的强制依赖。如果我们不添加此依赖,Drupal 在删除我们的模块时不会删除该内容类型。当站点开发人员决定不再需要此模块时,我们也不希望该内容类型继续存在。

# node.type.car_brand.yml
langcode: en
status: true
dependencies:
  enforced:
    module:
      - foobar # 这是我们在本示例中使用的模块名
name: 'Car Brand'
type: car_brand
description: 'Content type that can be used to provide additional information on <em>Car Brands</em>'
help: ''
new_revision: false
preview_mode: 1
display_submitted: true

 

foobar/config/install/field.field.node.car_brand.body.yml

此文件会将 body 字段添加到我们的内容类型。

# field.field.node.car_brand.body.yml
langcode: en
status: true
dependencies:
    config:
        - field.storage.node.body
        - node.type.car_brand
    module:
        - text
id: node.car_brand.body
field_name: body
entity_type: node
bundle: car_brand
label: Body
description: 'More specific information about the car brand.'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings:
    display_summary: true
field_type: text_with_summary

 

foobar/config/install/core.entity_view_display.node.car_brand.teaser.yml

此文件会告诉 Drupal 如何显示我们自定义内容类型的 teaser。

# core.entity_view_display.node.car_brand.teaser.yml
langcode: en
status: true
dependencies:
    config:
        - core.entity_view_mode.node.teaser
        - field.field.node.car_brand.body
        - node.type.car_brand
    module:
        - text
        - user
id: node.car_brand.teaser
targetEntityType: node
bundle: car_brand
mode: teaser
content:
    body:
        label: hidden
        type: text_summary_or_trimmed
        weight: 101
        settings:
            trim_length: 600
        third_party_settings: {  }
    links:
        weight: 100
hidden: {  }

 

foobar/config/install/core.entity_view_display.node.car_brand.default.yml

此文件会告诉 Drupal 默认情况下如何显示我们自定义内容类型的内容。

# core.entity_view_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
    config:
        - field.field.node.car_brand.body
        - node.type.car_brand
    module:
        - text
        - user
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
    body:
        label: hidden
        type: text_default
        weight: 101
        settings: {  }
        third_party_settings: {  }
    links:
        weight: 100
hidden: {  }

 

foobar/config/install/core.entity_form_display.node.car_brand.default.yml

此文件会告诉 Drupal 在创建我们自定义内容类型的新节点时,表单应如何显示。

# core.entity_form_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
    config:
        - field.field.node.car_brand.body
        - node.type.car_brand
    module:
        - text
        - user
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
    body:
        label: hidden
        type: text_textarea_with_summary
        weight: 101
        settings: {  }
        third_party_settings: {  }
    links:
        weight: 100
hidden: {  }

启用自定义内容类型

现在我们已经有了配置文件,需要告诉 Drupal 我们的新自定义内容类型。这是通过重新安装模块完成的。如果你的模块尚未启用,只需启用该模块即可。如果你的模块已经启用,先卸载模块再重新启用。
如果你现在进入“创建内容”页面,你会看到你可以创建一个新的内容类型节点——Car Brand