1

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?

1 Answer 1

0

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

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.