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(&$variables) {
$elements = $variables['elements'];
$paragraph = $elements['#paragraph'];
if(isset($paragraph) {
$paragraph_bundle = $paragraph->getParagraphType()->id();
switch($paragraph_bundle) {
case 'my_bundle':
$variables['elements']['#attached']['library'][] = 'my_theme/my-library-name';
break;
}
}
}