Score:3

Properly storing an array in a config object

in flag

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?

Score:4
in flag

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'
Kevin avatar
in flag
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 "
sonfd avatar
in flag
Updated with what I believe should resolve your issue.
Kevin avatar
in flag
That worked, thanks. I can now drop the strict check and the linter is fine.
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.