I can see in the standard install profile how to add a single value into a config file.

  // Enable the page cache and aggregation.
  config_set('system.core', 'preprocess_css', '1');
  config_set('system.core', 'preprocess_js', '1');
  config_set('system.core', 'cache', '1');

But, where I need to add an array, I am having difficulty. 

My specific use case, is that I'm trying to enable a filter in a text format. 

So, I have something like this in filter.format.filtered_html.json

{
    "filters": {
        "filter_image_align": {
            "status": 1,
            "weight": 5,
            "module": "filter",
            "settings": []
        },
        "filter_html": {
            "status": 1,
            "weight": -10,
            "settings": {
                "allowed_html": "<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <p> <img> <figure> <figcaption>"
            },
            "module": "filter"
        },
    },
}

And I would like to add this filter:

        "video_filter": {
            "status": 1,
            "weight": 0,
            "module": "video_filter",
            "settings": []
        }

Is this even a legit way to enable a filter?

Can anyone help me with the syntax?

Accepted answer

I agree with BWPanda about the better way to add a filter. As for your general question on how to add an array to a config file (which gets stored as json), have you tried:

 

$config = config('myconfig.settings');
$config->set('first_level.second_level', array('first_thing' => 'Primero', 'second_thing' => 'Segundo', 'third_thing' => 'Tercero'));
$config->save();

This results in:

 

{
    "_config_name": "myconfig.settings",
    "first_level": {
        "second_level": {
            "first_thing": "Primero",
            "second_thing": "Segundo",
            "third_thing": "Tercero"
        }
    }
}

Comments

I think that directly editing config is rarely, if ever, the best way to do something...

Perhaps try loading a format (with it's filters) using filter_format_load(), add the new filter array (by casting to an object), then save with filter_format_save():

$format = filter_format_load(FILTER_ID);
$format->filters['video_filter'] = (object) $FILTER_ARRAY;
filter_format_save($format);

Untested, but should put you on the right path...

I agree with BWPanda about the better way to add a filter. As for your general question on how to add an array to a config file (which gets stored as json), have you tried:

 

$config = config('myconfig.settings');
$config->set('first_level.second_level', array('first_thing' => 'Primero', 'second_thing' => 'Segundo', 'third_thing' => 'Tercero'));
$config->save();

This results in:

 

{
    "_config_name": "myconfig.settings",
    "first_level": {
        "second_level": {
            "first_thing": "Primero",
            "second_thing": "Segundo",
            "third_thing": "Tercero"
        }
    }
}

Thanks for those ideas. Here is what I'm doing now that works. 

This adds the video_filter to the config file. 

$format = filter_format_load('filtered_html');
$format->filters['video_filter'] = (object) $FILTER_ARRAY;
filter_format_save($format);

This enables the video_filter filter and makes sure the filters are in the correct order by setting weights for 3 filters.

config_set('filter.format.filtered_html', 'filters.video_filter.status', '1');
config_set('filter.format.filtered_html', 'filters.filter_html.weight', '-3');
config_set('filter.format.filtered_html', 'filters.video_filter.weight', '-2');
config_set('filter.format.filtered_html', 'filters.filter_url.weight', '-1');

I found another example from a previous project that looks like this:

 

$uuid2 = 'c6b14b4a-259e-41aa-a203-3b03710a7209';
$block2 = array(
  'content.' . $uuid2 => array(
    'plugin' => 'triplo_feed:news',
    'data' => array(
      'module' => 'triplo_feed',
      'delta' => 'news',
      'settings' => array(
        'title_display' => 'default',
        'title' => '',
        'style' => 'default',
        'block_settings' => [],
        'contexts' => [],
      ),
      'uuid' => $uuid2,
      'style' => array(
        'plugin' => 'default',
        'data' => array(
          'settings' => array(
            'classes' => '',
          ),
        ),
      ),
    ),
  ),
);
config_set_multiple('layout.layout.dashboard', $block );