I’m trying to send a variable to my JavaScript. I would like to send a class name to my twig template and the same class-name to my javascript from a function that returns a render array. The function is called from a routing yml to display a page from a link in a page (which is beside the point).
The returned render array is similar to the following one.
return [ '#theme' => 'theme_name', '#form' => $form, '#data' => [ 'customer' => $obj_customer, 'customer_subscription' => $obj_customer->getSubscription(), 'subscriptions_explanation' => $arr_subscription_text, ] ];
Passing data from PHP to javascript variable available to all pages in Drupal 8 says you can do something with '#attached' => ['drupalSettings' => ['do' => $something]] in hook_page_attachments(), but that doesn’t seem to apply to the method I am using.
How can I do in my case? Adding stylesheets (CSS) and JavaScript (JS) to a Drupal theme doesn’t seem to cover this use case.
In my JavaScript I define my JS functions inside:
(function ($, Drupal, drupalSettings) { Drupal.behaviors.portalCore = { someFunction() { ... } }; });
Is there perhaps a way to attach a variable to the portalCore from my example code?