How do I add module theme elements to drupalSettings I have a module "flot_d8" which is a wrapper for a javascript library. I would like to streamline the way this library is called from other modules (flot_examples in this case). I currently call it like this from src/Controller/FlotExample.php   

$data[] = [
  'data' =&gt; [
    [0, 12],
    [7, 12],
    NULL,
    [7, 2.5],
    [12, 2.5],
  ],
];
$div_id = 'placeholder';
$drupalsettings['flot_d8'][$div_id] = ['data' =&gt; $data];
$output[] = [
  '#attached' =&gt; [
    'drupalSettings' =&gt; $drupalsettings,
  ],
  '#theme' =&gt; 'flot_d8_my_template',
  '#data' =&gt; $series,
  '#options' =&gt; NULL,
  '#div_id' =&gt; $div_id,
];


Is there a way to force the flot_d8 module to add the $data to drupalSettings instead of passing this task to the flot_examples module in every single class function?

I tried this

function flot_d8_page_attachments_alter(&amp;$page) {
//$div_id = $output["#div_id'];
//$data = $output['#data'];
//$options = $output['#options'];
  $page['#attached']['drupalSettings']['flot_d8'][$div_id] = ['data' =&gt; $data, 'options' =&gt; $options];
}


But I know this isn't the right way to access $div_id, $data, and $options. Can I use the value in $output['#div_id'] in hook_page_attachments_alter()?
I created an Element within the parent module. This element includes code to add the data and options to drupalSettings. It also determines which libraries are needed and includes them.