Creating Shared Fields - NCIOCPL/cgov-digital-platform GitHub Wiki
Creating shared fields has two major parts. First the field's configuration is created, and then it's added to the cgov_core
installation profile in order to make it available when the CancerGov Drupal instance is set up.
Creating the field configuration
- Export a baseline config
- From inside the
web
container, run the command:drupal config:export --directory="../files-private/baseline-config" --remove-uuid --remove-config-hash
- From inside the
- Launch the Drupal UI
- In the Drupal UI, create a throwaway type.
Structure
>Content types
>Add content type
- Enter a name (e.g. "Junk Type")
- Click "Save and manage fields."
- Delete the automatically created
body
field.- Choose "Delete" from the dropdown button in the "Operations" column.
- Confirm the deletion.
- Click the "Add field" button.
- Choose a type for the new field.
- Enter the label (e.g. "My Text Field").
- Note: The label becomes the default "Machine name", you can edit this as needed. If the label was
"My Text Field" then the field's machine name would be
my_text_field
. You'll need this below.
- Note: The label becomes the default "Machine name", you can edit this as needed. If the label was
"My Text Field" then the field's machine name would be
- Click the "Save and continue" button.
- Set other field settings as needed.
- Click the "Save field settings" button.
- NOTE: There's no need to set the "Help text" at this point. Help text is actually defined when the field is attached to a node type.
- Click the "Save settings" button.
- At this point, you'll be back to the type's list of fields.
- Export the updated configuration.
- From inside the
web
container, run the command:drupal config:export --directory="../files-private/feature-config" --remove-uuid --remove-config-hash`
- NOTE: This is almost the same command as before, but the directory argument is changed.
- From inside the
Preserving the field configuration
The configuration export step places a collection of YAML files in $GIT_ROOT/files-private/feature-config
. You can compare this with the set in $GIT_ROOT/files-private/baseline-config
to see what's changed.
For the example of creating "My Text Field" (see above), the feature-config
directory will contain a new file field.storage.node.field_intro_text.yml
. This is the field's storage definition. (There will also be some other new and modified files associated with the new "Junk Type" node type as well connecting the node type with the field, but those are a separate discussion.)
- Copy
field.storage.node.field_intro_text.yml
to the$GIT_ROOT/docroot/profiles/custom/cgov_site/modules/custom/cgov_core/config/install/
directory. - Edit the new file to resemble the structure
langcode: en status: true dependencies: module: - node - text enforced: module: - cgov_core id: node.field_my_text_field field_name: field_my_text_field entity_type: node type: text_long settings: { } module: text locked: false cardinality: 1 translatable: true indexes: { } persist_with_no_fields: true custom_storage: false
- The changes you are making are:
- Add the
enforced:
property to note that the field is controlled bycgov_core
. - Change the
persist_with_no_fields
value totrue
so the field won't be immediately deleted because it's not being used by any node types.
- Add the
- The changes you are making are:
- Add a test for the existence of the field to
CGovFieldStorageTest
, located indocroot/profiles/custom/cgov_site/modules/custom/cgov_core/tests/src/Kernel/FieldStorage/