Overview

This is a memo on how to add content types and fields using Drupal custom modules.

The following two articles were helpful.

https://www.drupal.org/docs/drupal-apis/entity-api/creating-a-custom-content-type-in-drupal-8

https://www.digitalnadeem.com/drupal/how-to-create-content-type-fields-and-view-while-installing-custom-module-in-drupal-9-using-configuration-manager/

Car Brand Example

Following the first article introduced above, I was able to add a content type “Car Brand” with a “body” field.

Note that the above article skips the part about creating the custom module. First, create the following folder and file.

ndptvcaeayeomscprreckese:ra:i_igovfpemneot:o:roidsboCu1ianul.or:se0nt_Soramemqpulieremmoednutl:e^8^9

Adding Custom Fields

Using the above as reference, I was able to add a content type, but to add custom fields, I needed to add both a Field and a Field storage.

When I was unsure about how to write the YAML for Field storage, the second link introduced at the beginning was helpful.

https://www.digitalnadeem.com/drupal/how-to-create-content-type-fields-and-view-while-installing-custom-module-in-drupal-9-using-configuration-manager/

By using the “Configuration Manager” module, I was able to check the definitions of already registered Fields and Field storages.

Based on the above, let’s create a content type called “IIIF Media” and add a field for storing a string called iiif_image_url, and fields for storing numeric values called iiif_image_width and iiif_image_height. The following files are needed.

Content Type

modules/custom/foobar/config/install/node.type.iiif_media.yml

#lsdntdhnpdateayeeerinnapempslwesogteneecp_vpdcunfm::r:rileosdooieea.d:erd-'ipvwytencuIiti__y:tcelfIiismsprideoIfoioueeue::oF_nodb.nesbm:nemi:aMe::iired'tidiCf1tf#iaoae_anldmT'ts:eheedintistrau.iteysympltehefonramIeIIoFfMtehdeiam'odulewe'reusingforthisexample

Field: iiif_image_url (string)

Field storage

modules/custom/foobar/config/install/field.storage.node.field_iiif_image_url.yml

lsdifetsmlctipcatedinyeooarneunape:etptmcidcradrsgtenlietaasukdnestcunfmndt:ixs_leisxioosdooo_yn_eaednlesmd:erd-dn_sgl_s::aast_encueattsesclt:_s:tcelf.myr:neicfiawtrideofepignioatb{ioeue::oi:ents:rlyltrnesbe:ghies:eha:alf:tfe:}_grdinia1ne_eo2vlto:ild5esr_ide5:euffi_eiafifel_ialsiildemfssa_e:giemf_aauglresl_eurl

Note that by setting dependencies > enforced > module > {module name}, the storage is also deleted when the module is uninstalled. Without this setting, only the storage information remained after uninstalling.

The following article was helpful.

https://www.drupal.org/project/drupal/issues/3231028

!

On the other hand, having related content types and fields deleted when uninstalling a module can be problematic in some cases, so care must be taken with these settings.

Field

modules/custom/foobar/config/install/field.field.node.iiif_media.field_iiif_image_url.yml

#lsdifebldrtddsfatedinuaeereeeifnap:etnbsqaffteigtelidecunaatlecuncndtllrisuuidlosdoo_ye:irllln_dd:endn_:peattgt.enfeatItdt__syf:tci--.myiIi:avv:pirigiepiIobaaeeeue:fni:eiFnflll{:lnesioi:f:aeuud:edff_Il:ees.le_inmms:_}tnd.meoeaefcro.telddga{aidsyddeiellnetpi_aslg.oeaiUe}bir..iRaiaifiLcigiifkfeie_:_.flimn_dmeom_addeigiediea.ii_.fafufi_rieilelmldad_g_ieii_iiuifrf_l_iimmaaggee__uurrll.yml

Field: iiif_image_width (integer)

Field storage

modules/custom/foobar/config/install/field.storage.node.field_iiif_image_width.yml

lsdifetsmlctipcatedinyeooarneunape:etptusdcradrsgtenlietniukdnestcunfmndt:iszleisxioosdooo_ynieednlesmd:erd-dn_igg:::aast_encueatnsnlt:_s:tcelf.myt:encfiawtrideofepedooatb{ioeue::oi:eg:rrlyltrnesbe:emes:eha:alfrfae:}_grdinal1ne_eolto:ildsr_ideeuffi_eiafiel_ilsiidemfsa_:giemf_aawgliesd_etwhidth

Field

modules/custom/foobar/config/install/field.field.node.iiif_media.field_iiif_image_width.yml

#lsdifebldrtddsfatedinuaeereeeifnap:etnbsqafftmmpseigtelidecunaatiarulecuncndtllrisuuinxefdlosdoo_ye:irllln::ff_dd:endn_:peattgiit.enfeatItdt__snnxxyf:tci--.myiIi:avv:uu::pirigiepiIobaalleeeue:fni:eiFnflllll:lnesioi:f:aeuud:edff_Il:eei.le_inmms:_nnd.meoeaefcto.telddga{aedsyddeiellgetpi_asle.oeaiWe}brir..iiaiaifidcigiiftkfeie_h:_.flimn_dmeom_addeigiediea.ii_.fafwfi_iieidelmtldahd_g_ieii_iiwifif_d_itimhmaaggee__wwiiddtthh.yml

Summary

As a result, by installing the custom module “foobar,” I was able to create a content type called “IIIF Media” and add fields with string and integer field types, as shown below.

I hope this serves as a useful reference for adding content types and fields using custom modules.