Score:1

Correct schema for an Entity Reference in config settings

nl flag

Background

I am creating a schema for a custom module's config settings, which are saved from a simple settings form. It has some basic fields, such as a checkbox for a Boolean value for hiding some data. However, it also has an entity reference field (e.g. hide_data_terms) that allows selecting taxonomy terms from a specific bundle data_terms. It will save the term IDs to the settings YAML.

Here's my attempt at a my_module.schema.yml so far:

my_module.settings:
  type: mapping
  label: Settings
  mapping:
    hide_data:
      type: boolean
      label: 'Hide Data'
    hide_data_terms:
      type: entity_reference_selection.default:taxonomy_term
      label: 'Hide Data for Terms'

Problem

I'm not sure if entity_reference_selection.default:taxonomy_term is correct. Also, I have three terms saved from the settings page, and when using Config Inspector, it shows errors like this:

Name              | Label     | Type      | Value | Error
---------------------------------------------
hide_data_terms.0 | Undefined | undefined | 1234  | missing schema
hide_data_terms.1 | Undefined | undefined | 2034  | missing schema
hide_data_terms.2 | Undefined | undefined | 3287  | missing schema

my_module.settings.yml looks like this, with hide_data_terms containing an array of taxonomy term IDs:

hide_data: false
hide_data_terms:
  - 1234
  - 2034
  - 3287

What's wrong with the schema, if anything, or what should it be? I can't create a schema for each array element, because there can be any number of terms selected, or none of them.

Another Question:

The settings YAML does not have a "default config hash" for some reason. Most config YAMLs have it. Is that needed, and why isn't that in there?

Edit: I found the answer to the default config hash question. That is only used when the config is added on module install. See ConfigInstaller.php and search for default_config_hash.

unusedspoon avatar
aq flag
I think you'll need to use "sequence" to define that field. It allows for multiple unknown values. Have a look at https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-schemametadata#sequence-definitions
Score:4
us flag

Using entity_reference_selection.default:taxonomy_term type is incorrect as this config is used for field settings in the Entity reference type fields.

It is defined in the core/config/schema/core.data_types.schema.yml as entity_reference_selection.default:*.

So if you replace this type in your configuration with its definition it would look like this:

my_module.settings:
  type: mapping
  label: Settings
  mapping:
    hide_data:
      type: boolean
      label: 'Hide Data'
    hide_data_terms:
      type: mapping
      label: 'Hide Data for Terms'
      mapping:
        target_type:
          type: string
          label: 'Type of item to reference'
        target_bundles:
          type: sequence
          label: 'types'
          nullable: true
          sequence:
            type: string
            label: 'Bundle'
        sort:
          type: mapping
          label: 'Sort settings'
          mapping:
            field:
              type: string
              label: 'Sort by'
            direction:
              type: string
              label: 'Sort direction'
        auto_create:
          type: boolean
          label: 'Create referenced entities if they don''t already exist'
        auto_create_bundle:
          type: string
          label: 'Bundle assigned to the auto-created entities.'

Which is why you are getting the missing schema error.

As pointed out by @unusedspoon in the comment, you should use the sequence type for specifying an array of values.

The correct schema would look like this:

my_module.settings:
  type: mapping
  label: Settings
  mapping:
    hide_data:
      type: boolean
      label: 'Hide Data'
    hide_data_terms:
      type: sequence
      label: 'Hide Data for Terms'
      sequence:
        type: integer
        label: 'Term ID'
mbomb007 avatar
nl flag
Yes, this worked. Thank you.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.