Drupal 8以编程方式创建字段

Ale*_*ini 5 drupal drupal-field-api drupal-8 drupal-fields

我为Drupal 8创建了一个自定义模块,该模块允许用户选择一种Content类型,以便以编程方式添加一些字段。如何创建一些字段(在这种情况下为文本类型),并使用自定义模块将它们附加到内容类型?

一些帮助?

谢谢。

小智 3

Drupal 8 的结帐字段 API

它实现了多个函数,hook_entity_bundle_field_info可能就是您所需要的,这里是该钩子文档中的文本字段示例

function hook_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;
  }
}
Run Code Online (Sandbox Code Playgroud)

您可能还需要现场存储,查看hook_entity_field_storage_info