Creating a Custom field as a BaseFieldDefinition#
As with all field types, it's possible to create a custom field as a
BaseFieldDefinition
on an entity. The following example is for a custom field with two subfields of
type string, First Name (first_name) and
Last Name (last_name):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
The values to use in the settings can be determined as follows:
- Add the field to your entity in the field ui, on the admin pages. Configure as necessary.
- Export the configuration to the file system.
- Examine the exported configuration for the newly created field, and review:
field.storage.[ENTITY_TYPE].[FIELD_NAME].ymlfield.field.[ENTITY_TYPE]..[BUNDLE_TYPE].[FIELD_NAME].ymlcore.entity_form_display.[ENTITY_TYPE].[BUNDLE_TYPE].default.ymlcore.entity_view_display.[ENTITY_TYPE].[BUNDLE_TYPE].default.yml
- Use the relevant values from these files to populate the BaseFieldDefinition in the example above.
- The main keys you'll need to focus on are
columnsandfield_settings.