4

In a config object, I want to store an array of values. The keys matter. The stored value is coming from a checkboxes element, so the array would look something like:

['article' => 'article', 'page' => 'page']

Here is the config schema:

mymodule.settings:
  type: config_entity
  label: 'Mymodule Breadcrumb Settings'
  mapping:
    node_bundles:
      type: mapping
      label: 'Bundle machine names.'

This appears to work on the surface, but running a functional test results in:

Schema errors for mymodule.settings with the following errors: mymodule.settings:node_bundles.page missing schema

which makes me add:

protected $strictConfigSchema = FALSE;

then the tests pass - but then Drupal coding standards error is triggered.

What is the proper schema definition that I need?

0

1 Answer 1

5

This is a case for using a sequence. Unlike a mapping, keys are optional and don't need to be described by the schema.

From Configuration schema/metadata:

sequence: Property on the value of the sequence type, used to define an arbitrary list of defined values. In a sequence, keys are optional and may be integers or strings and are not explicitly mapped. Only the types of values must be defined in the schema.

Further down in that same documentation, we see examples for Sequence Defintions. The second example, Defining a new schema, looks a lot like your case.

Schema definition:

domain.language_negotiation:
  type: config_object
  label: 'Domain language negotiation'
  mapping:
    domain_language:
      type: sequence
      label: 'Domains'
      sequence:
        type: string
        label: 'Langcode mapping'

Example configuration supported by the above schema definition:

domain_language:
  example_local: en
  one_example_local: af
  two_example_local: af
  three_example_local: en
  four_example_local: en

For your specific case, you can update your schema definition like:

mymodule.settings:
  type: config_entity
  label: 'Mymodule Breadcrumb Settings'
  mapping:
    node_bundles:
      # Change from mapping to sequence
      type: sequence
      label: 'Bundle machine names.'
      # NEW: describe your sequence values.
      sequence:
        type: string
        label: 'whatever you would call the "values" of your node_bundles array'
3
  • I tried changing to a sequence, the test then reports: "Schema errors for mymodule.settings with the following errors: mymodule.settings:node_bundles.0 missing schema "
    – Kevin
    Commented Mar 9, 2022 at 14:17
  • Updated with what I believe should resolve your issue.
    – sonfd
    Commented Mar 9, 2022 at 14:26
  • That worked, thanks. I can now drop the strict check and the linter is fine.
    – Kevin
    Commented Mar 9, 2022 at 20:19

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.