Add new comment

How to attach a CSS or JS library to a View in Drupal 8?

 In .module file hook_views_pre_render().
<?php
/**
* Implements hook_views_pre_render().
*/
function hook_views_pre_render(ViewExecutable $view) {
  if (isset($view) && ($view->storage->id() == 'blog')) {
    $view->element['#attached']['library'][] = 'custom/blog';
  }
}
?>

Tags

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.