Score:1

Adding sequence item to configuration

mw flag

I have a configuration item in my custom module, part of which is a sequence, basically I'm attempting to create something akin to an array of items

my_module.schema.yml

my_module.settings:
  type: config_object
  label: 'Configuration'
  mapping:
    custom_fields:
      label: 'Custom Fields'
      type: sequence
      sequence:
        type: mapping
        label: 'Custom Field'
        mapping:
          field_label:
            type: string
            label: 'Field Label'
          field_key:
            type: string
            label: 'Field Key'

What I want to be able to do is add a new item to this 'list' as desired, but for the most part up to now (in other areas of my module) I've just been using $config->set(key, value) which will just overwrite the current setting, is there a method to add another value to the existing setting, or do I have to load the existing configuration, add it in manually, then save over the whole lot?

Score:0
mw flag

turns out, yes, you can retrieve the existing set of variables and add to it, but I'm not saying for certain that this is the correct, or best way to do it

// Load the current configuration and add this to it
$custom_fields = $config->get('custom_fields');
$custom_fields[] = array(
  'field_label' => 'some label',
  'field_key' => 'some_key',
);
$config->set('custom_fields', $custom_fields);

The above works fine, if anyone encounters this and wants to see how to do it, or anyone knows a better way, please feel free to comment

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.