I'm having a very hard time figuring out how in a generic way (so not a specific display plugin, but say in a module that can only implement hooks) to add #attached information to a View's output so that it will get rendered and output. Specifically I want to add libraries to Views using ['#attached']['library'][] = array('module', 'library-name').
I see that #1748164: Use #attached to add all CSS and JS was open at one point and set to be backported to Drupal 7, but was eventually marked as a duplicate of #1811828: Use #attached to find the css/js of a view which looks like it contains D8 only changes that cannot be backported.
Am I missing some kind of hook or preprocess that I can use? Currently I have to use the following which will have compatibility problems with things like AJAX views:
function mymodule_views_pre_render($view) {
// Add our library to all views (this is silly, but I just want to test adding a library).
drupal_add_library('mymodule', 'mylibrary');
}
Comments
Comment #1
moshe weitzman commentedSome workarounds focused on blocks - #1460766: [PP-1] Fix block cache to use Attachment Collectors to prevent brokenness with drupal_add_* calls, #1534490: Make block cache cids alterable
Comment #2
mariacha1 commentedComment #3
mariacha1 commentedComment #4
stewart.adam commentedIf your View uses exposed forms, a workaround is to implement hook_form_views_exposed_form_alter() to modify $form['#attached']. One can check against $form['#id'] to identify the current view.
Comment #5
msypes commentedI'm thinking this thread is more a support request than a bug report, isn't it?
And in that vein, is there a proper method to add #attached to a view with a hook? Currently, I'm using the older, and soon-to-be-deprecated drupal_add_xxx(). Just trying to stay up to date.
Comment #6
plazik commentedI'm using this code in
my_module_preprocess_views_view(&$vars):Comment #7
plazik commentedFor D8:
Comment #8
doublejosh commentedUsing the exposed form seems like the closest approach... if you're using filters.
Another option is to use the Context Add Assets module.
Comment #9
sgdev commented@doublejosh, thanks for the suggestion about Context Add Assets, although we're not using Context and just want to attach JavaScript to a view without exposed filters.
We decided to essentially do what Dave Reid mentioned in the original post, using
drupal_add_libraryin atemplate_preprocessfor the View.Comment #10
taggartj commented@plazik His works for modules as well for D8 as this hook works in modules.