I have a custom module and in it’s .module file I add a value to Drupal settings:
$variables['#attached']['drupalSettings']['my module']['color_body'] = '#dd0000';
I have a named js function and try to access the value:
function loadcal() { var backgroundColor = drupalSettings.mymodule.color_body; }
I receive the error:
Uncaught TypeError: Cannot read properties of undefined (reading 'my module') at Object.attach (loadcal.js?rb7yo3:52:37)
All of the working examples of using drupalSettings seem to be with anonymous functions:
(function ($) {}
I can refactor to do this as a DOMready function, but would prefer this function to be callable. I have tried:
(function loadcal($) {}
but that leaves loadcal() not being found when it is called.