将自定义字段编程到你的内容类型中
有时候,当你从自定义模块中导出内容类型时,你还可以为该内容类型添加相关的字段。能够自动创建字段使你能够在多个站点上删除并重新安装模块,而不会留下多余的字段,并确保你不会忘记添加它们。我们将介绍两种方法来将这些字段添加到你的代码库中。
前提条件
- 已安装 Drupal 8.0.x。
- 有一个自定义模块(在本例中模块名称为 foobar)。
- 模块中有一个自定义内容类型(请按照父页面中的说明操作。内容类型名称为 Car Brand)。
为我们的内容类型添加额外字段
有两种方法可以完成这一步。你可以使用用户界面来创建字段并将配置导出为文件,或者你可以直接自己编写文件。我喜欢使用第一种方法,只是为了确保不会遗漏 Drupal 添加字段时所需的任何内容,不过导出的字段会以 “field_” 开头,而不是 “foobar_car_brand_”,以便将它们绑定到我们的自定义内容类型。在本页中,我们将介绍如何在自定义模块中手动编写字段代码。
在你的模块中手动编写字段代码
如前所述,这种方法需要更多的知识,以及你对字段应做什么和应如何显示的明确理解。每个添加的字段都需要有两个 YAML 文件,包含配置信息。它们的命名如下:field.field.node.car_brand.field_brand_information.yml 和 field.storage.node.field_brand_information.yml。
删除自定义内容类型
如果你在上一页末尾已经启用了 foobar 模块,现在请继续将其删除。
foobar/config/install/field.storage.node.field_brand_information.yml
此文件告诉 Drupal 它需要创建我们的字段。
# field.storage.node.field_brand_information.yml
langcode: en
status: true
dependencies:
module:
- node
- text
id: node.field_brand_information
field_name: field_brand_information
entity_type: node
type: text_with_summary
settings: { }
module: text
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
foobar/config/install/field.field.node.car_brand.field_brand_information.yml
此文件告诉 Drupal 它需要将我们的字段附加到我们的内容类型。
# field.field.node.car_brand.field_brand_information.yml
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_brand_information
- node.type.car_brand
module:
- text
id: node.car_brand.field_brand_information
field_name: field_brand_information
entity_type: node
bundle: car_brand
label: 'Brand Information'
description: 'More specific information about the car brand'
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
display_summary: false
field_type: text_with_summary
foobar/config/install/core.entity_form_display.node.car_brand.default.yml | foobar/config/install/core.entity_view_display.node.car_brand.default.yml
这两个文件需要在上一页的基础上更新,以便包含所有新添加的字段。我添加了两个额外字段,分别叫 “Additional_field_1” 和 “Additional_field_2”,这样你可以看到多个字段时的样子。
注意:如果你不想创建额外的字段,请删除下面代码中这些字段的依赖和信息。
# core.entity_view_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
config:
- field.field.node.car_brand.field_brand_information
- field.field.node.car_brand.field_additional_field_1
- field.field.node.car_brand.field_additional_field_2
- node.type.car_brand
module:
- file
- text
- user
_core:
default_config_hash: Nfnv6VMugBKl6EOqi_U0I_LQ1ZQpbNDN3a9GXHWUBz4
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
field_brand_information:
weight: 101
label: above
settings: { }
third_party_settings: { }
type: text_default
field_additional_field_1:
weight: 103
label: above
settings:
link_to_entity: false
third_party_settings: { }
type: string
field_additional_field_2:
weight: 102
label: above
settings: { }
third_party_settings: { }
type: file_default
links:
weight: 100
hidden: { }
# core.entity_form_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
config:
- field.field.node.car_brand.field_brand_information
- field.field.node.car_brand.field_additional_field_1
- field.field.node.car_brand.field_additional_field_2
- node.type.car_brand
module:
- file
- path
- text
_core:
default_config_hash: qZE-qJ04DTTNggVVQdVOPQmpE_I69GQ_LqB32kXivVg
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
created:
type: datetime_timestamp
weight: 2
settings: { }
third_party_settings: { }
field_brand_information:
weight: 7
settings:
rows: 9
summary_rows: 3
placeholder: ''
third_party_settings: { }
type: text_textarea_with_summary
field_additional_field_1:
weight: 6
settings:
size: 60
placeholder: ''
third_party_settings: { }
type: string_textfield
field_additional_field_2:
weight: 8
settings:
progress_indicator: throbber
third_party_settings: { }
type: file_generic
path:
type: path
weight: 5
settings: { }
third_party_settings: { }
promote:
type: boolean_checkbox
settings:
display_label: true
weight: 3
third_party_settings: { }
sticky:
type: boolean_checkbox
settings:
display_label: true
weight: 4
third_party_settings: { }
title:
type: string_textfield
weight: 0
settings:
size: 60
placeholder: ''
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 1
settings:
match_operator: CONTAINS
size: 60
placeholder: ''
third_party_settings: { }
hidden: { }
启用自定义内容类型
现在你应该启用 foobar 模块。如果你现在访问“创建内容”页面,你会看到可以创建新的内容类型 “Car Brand” 的节点,并且它会包含我们新添加的名为 “Brand Information” 的字段。
我们将在下一页中介绍如何通过用户界面添加这些字段。