Created
August 12, 2016 11:40
-
-
Save steffenr/a66d4d67af70bd866c5af1b459a46cb9 to your computer and use it in GitHub Desktop.
Attach a CSS or JS library to a View in Drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
custom_view: | |
css: | |
component: | |
css/custom_view.css: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_views_pre_render(). | |
*/ | |
function custom_views_pre_render(ViewExecutable $view) { | |
if (isset($view) && ($view->storage->id() == 'my_view')) { | |
$view->element['#attached']['library'][] = 'custom/custom_view'; | |
} | |
} |
you can check this module "https://www.drupal.org/project/views_attach_library" to attach CSS or JS library to a View in Drupal 8
I understand that custom_view is the library name as defined in mymodule.libraries.yml. But where is custom defined in view->element['#attached']['library'][] = 'custom/custom_view'; ? Is that hardcoded? I had the impression it had to be the module name.
@com2 the first part of the library ("custom", here) is the name of the module or theme which provides the library. So for the example above, it should be mymodule/custom_view
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this works for me