I have defined fields using the UI, and some more fields using YAML files in my custom module, e.g.:

The field storage is defined in field.storage.node.my_heading:

langcode: en
status: true
      - my_module
id: node.my_heading
field_name: my_heading
entity_type: node
type: string
settings: {  }
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

The field instance (which attaches a field to an entity) is defined in field.field.node.my_content_type.my_heading:

langcode: en
status: true
    - field.storage.node.my_heading
    - node.type.my_content_type
id: node.my_content_type.my_heading
field_name: my_heading
entity_type: node
bundle: my_content_type
label: 'Heading'
description: 'An optional heading'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: string

After installing this custom module, how do I get the field configuration in code?


The terminology is a bit confusing (as usual with Drupal), but here goes:

Field storage configurations are defined by FieldStorageConfig. To load one, do this:

$field = Drupal::entityTypeManager()->getStorage('field_storage_config')->load('node.my_field');

Field instance configurations are defined by FieldConfig. To load one, do this:

$field_instance = Drupal::entityTypeManager()->getStorage('field_config')->load('node.my_content_type.my_field');
  • Helpful. How to read the field default value? Jul 27 '19 at 18:28
  • @RajeshVishwakarma The default value is part of the field instance configuration. The code to load that is shown in my answer. Jul 6 '20 at 8:11

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.