It does apply to the method you are using. You can add metadata like #cache or #attached to any render element (#theme, #type, #markup, #plain_text): return [ '#theme' => 'theme_name', '#form' => $form, '#data' => [ 'customer' => $obj_customer, 'customer_subscription' => $obj_customer->getSubscription(), '...


As described in Render API overview (the Attaching libraries in render arrays section), #attached can be always used in a render array. If your function/method returns a render array, it could return the following render array. return [ '#attached' => [ 'drupalSettings' => ['do' => $something], ], '#theme' => 'theme_name', '#form' =&...


Following the tips in this post, I changed the code as follows and it worked: (function ($, Drupal) { Drupal.behaviors.myBehavior = { attach: function (context, settings) { $('input.countme').change(function(){ var a = $("input.countme:checked").length // count all checked checkboxes with the css class "countme" ...


After some debugging I have found I can access drupalSettings object and It has a property addToCart with all the data I need.


Check out hook_views_pre_render https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_render/7.x-3.x That provides a view object where you could retrieve the machine name of your view and add your drupal_add_js when its the relevant view being loaded

Only top voted, non community-wiki answers of a minimum length are eligible