Score:1

Programmatically add new fields to content type

jp flag

I have a custom module that has a form that allows the user to select various content types. I would like to create fields for those content types programmatically so that after the form is saved, it will add the fields for all of the content types that the user selected.

So my question is, is there a way to programmatically create fields and then save them to the content types? Any direction (tutorials, etc) would be greatly appreciated. I've searched for a while, but most of what I'm seeing is for d7 which obviously isn't what I'm looking for since this is for d9.

4uk4 avatar
cn flag
See https://drupal.stackexchange.com/questions/222260/add-content-type-field-programmatically
id flag
I’m voting to close this question because it duplicates https://drupal.stackexchange.com/questions/222260/add-content-type-field-programmatically
user1470118 avatar
jp flag
That reference adds a body field when the content type is created. That's not really what I'm looking for. How would I even translate that to a date field (for instance)?
Score:0
cn flag

For the PHP commands necessary to programmatically add new fields to content types see:

Add content type field programmatically

That reference adds a body field when the content type is created. When you want to add fields to existing content types then load them in a loop:

foreach (NodeType::loadMultiple() as $type) { }

How would I even translate that to a date field (for instance)? To get the necessary plugin names and settings add the field in UI temporarily. Export the configuration and compare it with the previous configuration. You find two new YAML files, the field storage (in UI the field settings) and the bundle field definition (in UI the field edit). The display and form settings are added to existing YAML files. Make a diff to find the new entries. With this information you can fill the parameter lists of the PHP methods and, if you want, you can use the YAML file of the field storage like described in the linked answer.

Score:-1
fr flag

How necessary is the part of it being programmed by yourself? It sounds like having a large overlap with the Entity Construction Kit. Probably, things might work with less own code than you expect.

If your use case is really that special, I still recommend you this module as reading material. It might contain the snippets that you need.

Kevin avatar
in flag
Once an entity is defined in code, it can either have base fields added or fields added to it from the UI.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.