I wrote a custom module recently where I needed to programmatically attach a field; similar to how a Body field gets added to content types.
If you create a content type, via the “Content types” page, a Body field is automatically added to the content type. If you don’t need the field just delete it, but the default functionality is to have it added.
I wanted this same functionality in my custom module; when an entity is created a field is programmatically attached to it.
So I reverse engineered how the Body gets added to content types. In the Node module, the Body field is exported as field.storage.node.body.yml and the field is attached using the node_add_body_field function.Continue reading