【问题标题】:Module Hook to add extra field in drupal 8 content types在 drupal 8 内容类型中添加额外字段的模块挂钩
【发布时间】:2016-05-06 08:10:46
【问题描述】:

我想在 Drupal 8 内容类型中添加一个带有自定义模块的额外字段,但我没有任何钩子可做。

下面是我正在使用的钩子,但这对我想要的结果没有帮助:

function nodeclass_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
 // Add a property only to nodes of the 'article' bundle.
  if ($entity_type->id() == 'node' && $bundle == 'article') {
    $fields = array();
    $fields['mymodule_text_more'] = BaseFieldDefinition::create('string')
      ->setLabel(t('More text'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\mymodule\EntityComputedMoreText');
    return $fields;
  }
}

【问题讨论】:

  • 没有人来回答这个问题??
  • 这可能还不够,你还需要 hook_entity_field_storage_info。
  • 您也可以观看:drupal.stackexchange.com/questions/220175/… 并且文档说您需要 hook_entity_field_storage_info 或者您需要覆盖现有的字段库。你不做这两个中的任何一个。

标签: module hook drupal-8


【解决方案1】:

在您的模块文件夹中,创建以下文件夹 模块/my_module/config/install/

然后在你的模块文件夹中创建以下文件。

my_module/config/install/field.field.node.article.field_text_more.yml

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_text_more
    - node.type.article
id: node.article.field_text_more
field_name: field_text_more
entity_type: node
bundle: article
label: 'More text'
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: string

my_module/config/install/field.storage.node.field_text_more.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: node.field_text_more
field_name: field_text_more
entity_type: node
type: string
settings:
  max_length: 255
  is_ascii: false
  case_sensitive: false
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

然后重新安装您的模块,它会将新字段“更多文本”添加到“文章”内容类型中。

注意:如果您希望将字段添加到任何其他内容类型,请更改文件名并更新 field.field.node.article.field_text_more.yml 中的“bundle: article”行。 p>

【讨论】:

  • 如果你有一个有内容的模块,这不是一个好主意,因为当你禁用模块时,所有相关的内容都会被删除
  • 这并不完全正确。 Drupal 不允许您禁用这样的模块。无论在何处使用,您都必须先删除该字段。
猜你喜欢
  • 2016-09-11
  • 1970-01-01
  • 2021-05-07
  • 2016-06-25
  • 2017-11-13
  • 1970-01-01
  • 2023-04-11
  • 2013-10-13
  • 1970-01-01
相关资源
最近更新 更多