What is the proper way to add a non-base textarea field to the entity form of a custom content entity?

comment and user modules have non-base fields that display in their entity form (comment_body and picture), but it appears these fields are added to the form somewhere besides the main entity form method they use.

In my module, I've created a field and instance inside hook_install like this:

function mymodule_install() {
  $field = array(
    'name' => 'myfield',
    'entity_type' => 'myentitytype',
    'type' => 'text_long',
    'module' => 'text',
    'settings' => array(),
    'cardinality' => 1,
    'locked' => FALSE,
    'indexes' => array(),
  entity_create('field_entity', $field)->save();

  $instance = array(
    'field_name' => 'myfield',
    'entity_type' => 'myentitytype',
    'bundle' => 'myentitytype',
    'label' => 'Field title',
    'description' => t('Field description.'),
    'required' => TRUE,
    'default_value' => array(),
    'settings' => array(
      'text_processing' => '0'
  entity_create('field_instance', $instance)->save();

I think if I could be pointed to how comment_body gets added to the comment entity form, or picture to the user entity form, I'd be able to resolve this.


You can ship a locked Field with yml only, see core/modules/forum/config/field.*. Ie just make the fields in the ui and copy the yml files from your active store to a config folder inside your module.

This ships a locked boolean field for the container flag. You'll also need form and entity display entities, but you can create them in the ui and grab the yml for them too.

| improve this answer | |
  • Interesting I guess, but did you mean to post this here? It doesn't seem to address the question in any way – Clive Oct 7 '13 at 9:04
  • 1
    It is related but maybe not complete. What's probably missing in this case are the form and view display configurations, see entity.display.comment.node__comment.default.yml and entity.form_display.comment.node__comment.default.yml, maybe also a view mode: entity.view_mode.comment.full.yml – Berdir Oct 7 '13 at 9:38
  • Thanks guys. After considering this, I'm thinking it might be more of a field API question. I'm not just trying to find a way to pull it off, I'm trying to learn D8 so I can answer these questions myself. I think I should be able to do this without configs. This info gives me some direction though. I'll spend some more time with it focusing more on field API. – Coder1 Oct 7 '13 at 16:28

Parts of how to do this have chanaged or are deprecated. The arrays are mostly correct (name is now field_name) (look in the yml for example values if you don't know what to use) but the functions are now:



You can find out how this all works including setting the field on form displays and view displays in /core/modules/field_ui/src/Form/FieldStorageAddForm.php.

| improve this answer | |

The solution is to add the following code after the field instance is created.

This is what the config files named entity.form_display* are based on.

entity_get_form_display('myentitytype', 'myentitytype', 'default')
->setComponent('myfield', array(
  'type' => 'text_long',
  'settings' => array(
    'text_processing' => '0'
  'weight' => 5,
| improve this answer | |
  • 1
    This is correct, but you don't need this code anymore after it has been generated initially (you can create it in the UI). Just copy the yml files you need into the config folder of your module and it will be created when the module is installed. – Berdir Oct 8 '13 at 10:25
  • Thanks for the confirmation Bedir, that's what I needed to know. If configs are the "proper" way to deliver a field in D8, then it sounds like DX could be improved with a developer field UI to generate the field configs on any entity, rather than this process, or creating the field on a node, and modifying configs. – Coder1 Oct 8 '13 at 17:40
  • Well, you can use the normal field UI to create the field you want. Then you just need to copy all the affect config files. One way to find them would be to e.g. copy the active config directory to staging first and then diff or something like that. – Berdir Oct 9 '13 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.