5

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
dependencies:
  enforced:
    module:
      - 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
dependencies:
  config:
    - 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?

8

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');

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.