I'm trying to create a module that creates its own content types during installation. I understand that I need to use the Configuration API and the Entity API, but I'm having trouble adding Drupal-included fields to my custom type. From what I understand, I need to have a file for each field called field.field.node.content_type.field_type.yml. Am I correct in my understanding? If so, why are their two fields and where can I find the options to place in that configuration file? I have tried scouring through the API and I can't seem to find what I need.

This is the tutorial I'm following: https://www.drupal.org/docs/8/api/entity-api/creating-a-custom-content-type-in-drupal-8


My suggestion is to use the Core Configuration Manager to "automatically" generate all the options you need. See https://www.drupal.org/docs/8/configuration-management/managing-your-sites-configuration

The approach I'd use would be to create the new content types and all their fields through the UI. Then go to Configuration -> Development -> Configuration synchronization -> Export (tab) -> Single item

From there, you just need to select your various pieces of content one-by-one. For example, if you select "Content type" from the Configuration type menu and then Article (or your custom content type), you'll see the configuration you'd put in the .yml file in your custom module config/install folder. Just eliminate that first uuid: line. If you scroll down past the configuration text area, you'll see the name of the file name you need. For article, it's "node.type.article.yml"

Besides the content types, you'll also need the Field and Field storage configuration types (and probably Entity form display and Entity view display). Pay attention to the config dependencies to make sure you get all the pieces of configuration you need.

Once you get everything you need into configuration files, you can delete everything you created through the UI, install (or reinstall) your custom module and then check whether everything is as you want it. If it's not, you can make changes through the UI and export the config again to fix your custom module configuration yaml files. Or you can just edit the files directly once you see how everything is organized, especially if you're just making little changes like the label or description text for a field.

For me, this approach was much less painful than "scouring through the API", trying to find all the various keys and possible values.

  • Thank you! Should I also remove the default_config_hash from the node.type.content_type.yml files? Jun 29 '17 at 23:58
  • 1
    Yes, sorry, I missed that detail! I removed the "_core:" line as well as the default_config_hash line that follows.
    – user72086
    Jun 30 '17 at 1:02

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.