By beakerboy on
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' => [
[0, 12],
[7, 12],
],
];
$div_id = 'placeholder';
$drupalsettings['flot_d8'][$div_id] = ['data' => $data];
$output[] = [
'#attached' => [
'drupalSettings' => $drupalsettings,
],
'#theme' => 'flot_d8_my_template',
'#data' => $data,
'#options' => NULL,
'#div_id' => $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 was thinking something like this:
function flot_d8_page_attachments_alter(&$page) {
//$div_id = $output["#div_id'];
//$data = $output['#data'];
//$options = $output['#options'];
$page['#attached']['drupalSettings']['flot_d8'][$div_id] = ['data' => $data, 'options' => $options];
}
But I know this isn't the right way to access $div_id, $data, and $options. Can I somehow access the value of $output['#div_id'] in hook_page_attachments_alter()?
Comments
Will Preprocess Functions Work?
Is there a preprocess function which would be able to see both the theme variables (like template_preprocess_HOOK does) as well as having access to the $page data that hook_page_attachments_alter is passed? Is it good form to make one global somehow? If so, How?