How to add library to Paragraph bundle using hook_preprocess_theme? I wanted to add a library to a specific paragraph bundle. I know I can do {{ attach_library('my_theme/my-library-name') }} using twig, but that means I have to create a file for each bundle. The goal is to be able to use just the my_theme.theme file to add the library.

Question

How to add library to Paragraph bundle using hook_preprocess_theme?

Here is what we tried, but it didn't work even after cache clear.

function mytheme_preprocess_paragraphs(&amp;$variables) {
  $elements = $variables['elements'];
  $paragraph = $elements['#paragraph'];
  if(isset($paragraph) {
    $paragraph_bundle = $paragraph-&gt;getParagraphType()-&gt;id();
    switch($paragraph_bundle) {
      case 'my_bundle':
        $variables['elements']['#attached']['library'][] = 'my_theme/my-library-name';
        break;
    }
  }
}
You have to do like this instead:

$variables['#attached']['library'][] = 'my_theme/my-library-name';


So you attach the library directly to $variables instead.