In Drupal 8, how do I get all CSS and JS that is attached to a page? In Drupal 7, I could use drupal_add_css() or drupal_add_js() without arguments, to get an array containing a list of all the files/inline added to a page.

I'm developing a module that will display a "preview" of the block on block configuration page. The method I chose is displaying the block code in a <iframe>, but to be properly styled, I need to output all styles with it. This is why I want to read all possible CSS/JS in this "preview" page controller.

  • 1
    refer drupal.org/theme-guide/8/assets
    – Abin
    Commented Nov 30, 2015 at 8:31
  • You can use hook_css_alter or hook_js_alter but it depends on what you want to achieve.
    – user21641
    Commented Nov 30, 2015 at 9:27
  • 1
    I recommend that you edit your question and ask what you actually want to achieve. Chances are that there is better way to achieve whatever it is exactly that you want to do.
    – Berdir
    Commented Nov 30, 2015 at 20:15
  • 1
    I think @Berdir was looking for more of the why do you want to get these lists? It's unusual to need them, which is possibly why there's no obvious way to get them (at least as far as I can see after a couple of minutes browsing the code). If you can share your ultimate goal, it might be easier to point you in the right direction
    – Clive
    Commented Nov 30, 2015 at 23:33
  • 1
    Exactly. What do you want to do with those files?
    – Berdir
    Commented Dec 1, 2015 at 0:06

1 Answer 1


You cannot get a list of css/js files because it is constructed very late in the render process.

However, since you want to use an iframe to achieve that goal, you can get it through the normal loading order of a drupal page.

The following is a proof of concept controller method you can use to display a block in a simplified html mode.

public function preview($block_id) {
  /** @var BlockInterface $block */
  $block = Block::load($block_id);

  /** @var BareHtmlPageRendererInterface $bare_html_page_renderer */
  $bare_html_page_renderer = \Drupal::service('bare_html_page_renderer');
  $content = [
    'preview-child' => \Drupal::entityTypeManager()->getViewBuilder('block')->view($block)

  return $bare_html_page_renderer->renderBarePage($content, 'preview-title', 'preview-theme');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.