1

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.

1

1 Answer 1

4

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'
1
  • Yes, this worked. Thank you.
    – mbomb007
    Commented Dec 7, 2022 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.